mongodb - Robomongo 查询以返回 id 列表
问题描述
我想在 Mongo 中查询我的数据库,然后能够复制并粘贴查询返回的 id 列表。
我知道我可以投射_id
类似的东西
db.getCollection('mymodel').find({}}, { _id: 1 })
但我希望能够将结果复制并粘贴为 id 数组,有没有办法用 Robomongo/Mongo 实现这一点?
解决方案
Is this query you want?
Using aggregate
add all _id
s to a set:
db.collection.aggregate([
{
"$group": { "_id": null, "ids": { "$addToSet": "$_id" } }
},
{
"$project": { "_id": 0 }
}
])
And the ouput is similar to this, an array called ids
with all id
:
"ids": [
ObjectId("5a934e000102030405000000"),
ObjectId("5a934e000102030405000004"),
ObjectId("5a934e000102030405000001"),
ObjectId("5a934e000102030405000005"),
ObjectId("5a934e000102030405000003"),
ObjectId("5a934e000102030405000002")
]
You can use $match
to filter the documents you want to get the id
like this example.
推荐阅读
- laravel - Laravel 护照 / 允许客人使用 api 路由 / 绕过 Laravel api 中间件
- python-3.x - TypeError:“按钮”对象不可调用(pynput)
- java - Junit测试用例未使用maven运行
- go - UnhandledPromiseRejectionWarning:错误:nonce 生成函数失败或私钥在 Ganache 中无效
- python-3.x - 当我单击关闭然后再次单击运行时,我收到此错误“RuntimeError:线程只能启动一次”
- c# - 如何使用 Excel 互操作保存启用宏的工作簿 (xlsm)?
- c# - C#从数组中删除一个数字
- openedge - 使用参数的方法时出现运行时错误
- d3.js - 条形图中的分类标签部分隐藏
- php - 以数组格式传递数字 - Laravel