如何使用MongoDb id数组获取多个文档? [英] How to get multiple document using array of MongoDb id?

查看:59
本文介绍了如何使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆