如何使用MongoDb id数组获取多个文档? [英] How to get multiple document using array of MongoDb id?
问题描述
我有一个id数组,我想一次获取它们的所有文档.为此,我正在写,但它返回0条记录.
I have an array of ids and I want to get all document of them at once. For that I am writing but it return 0 records.
如何使用多个ID进行搜索?
How can I search using multiple Ids ?
db.getCollection('feed').find({"_id" : { "$in" : [
"55880c251df42d0466919268","55bf528e69b70ae79be35006" ]}})
我可以通过传递单个ID来获取记录,例如
I am able to get records by passing single id like
db.getCollection('feed').find({"_id":ObjectId("55880c251df42d0466919268")})
推荐答案
MongoDB对类型敏感,这意味着1
与'1'
不同,"55880c251df42d0466919268"
和ObjectId("55880c251df42d0466919268")
也是如此.后一种是ObjectID类型,但不是str,也是MongoDB文档的默认_id
类型.
MongoDB is type sensitive, which means 1
is different with '1'
, so are "55880c251df42d0466919268"
and ObjectId("55880c251df42d0466919268")
. The later one is in ObjectID type but not str, and also is the default _id
type of MongoDB document.
您可以在此处找到更多信息.
You can find more information about ObjectID here.
只需尝试:
db.getCollection('feed').find({"_id" : {"$in" : [ObjectId("55880c251df42d0466919268"), ObjectId("55bf528e69b70ae79be35006")]}});
这篇关于如何使用MongoDb id数组获取多个文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!