node.js 如何在 Mongoose 中执行 id 数组查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5818303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I perform an id array query in Mongoose?
提问by TIMEX
Let's say I have a model called User.
I have an array with object Ids.
假设我有一个名为User. 我有一个带有对象 ID 的数组。
I want to get all User records that "intersect" with the array of Ids that I have.
我想获得所有与我拥有的 Id 数组“相交”的用户记录。
User.find({ records with IDS IN [3225, 623423, 6645345] }, function....
回答by kberg
Here is a mongoosey way to use the $in operator.
这是使用 $in 运算符的猫鼬方式。
User.find()
.where('fb.id')
.in([3225, 623423, 6645345])
.exec(function (err, records) {
//make magic happen
});
I find the dot notation quite handy for querying into sub documents.
我发现点符号对于查询子文档非常方便。
回答by neebz
You need to use the $in operator >
您需要使用 $in 运算符 >
https://docs.mongodb.com/manual/reference/operator/query/in/#op._S_in
https://docs.mongodb.com/manual/reference/operator/query/in/#op._S_in
For example:
例如:
Users.find( { "fb" : { id: { $in : arrayOfIds } } }, callback );
回答by Sagiv Ofek
User.where({ records: { $in: [3225, 623423, 6645345] } }, function ...
more info here: http://docs.mongodb.org/manual/reference/operator/query/
更多信息:http: //docs.mongodb.org/manual/reference/operator/query/
回答by Alex Montoya
For me, work this way
对我来说,这样工作
IDs=["5b00c4b56c7fb80918293dd9","5b00c4b56c7fb80918293dd7",...]
const users= await User.find({records:IDs})
回答by SnnSnn
Ids is the array of object ids:
Ids 是对象 ID 的数组:
const ids = [
'4ed3ede8844f0f351100000c',
'4ed3f117a844e0471100000d',
'4ed3f18132f50c491100000e',
];
With callback:
带回调:
User.find().where('_id').in(ids).exec(callback);
With async function:
具有异步功能:
records = await User.find().where('_id').in(ids).exec();

