首页 > 解决方案 > Robomongo 查询以返回 id 列表

问题描述

我想在 Mongo 中查询我的数据库,然后能够复制并粘贴查询返回的 id 列表。

我知道我可以投射_id类似的东西

db.getCollection('mymodel').find({}}, { _id: 1 })

但我希望能够将结果复制并粘贴为 id 数组,有没有办法用 Robomongo/Mongo 实现这一点?

标签: mongodbrobo3t

解决方案


Is this query you want?

Using aggregate add all _ids 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.


推荐阅读