题
假设我有一个叫做的模型 User
。我有一个带有对象ID的数组。
我想获得与我拥有的一系列ID“相交”的所有用户记录。
User.find({ records with IDS IN [3225, 623423, 6645345] }, function....
解决方案
您需要在操作员中使用$
https://docs.mongodb.com/manual/reference/poerator/query/query/in/#op._s_in
例如:
Users.find( { "fb" : { id: { $in : arrayOfIds } } }, callback );
其他提示
这是一种使用$运算符的词汇。
User.find()
.where('fb.id')
.in([3225, 623423, 6645345])
.exec(function (err, records) {
//make magic happen
});
我发现DOT表示法非常方便地查询子文档。
User.where({ records: { $in: [3225, 623423, 6645345] } }, function ...
更多信息在这里: http://docs.mongodb.org/manual/reference/operator/query/
对我来说,这样做
IDs=["5b00c4b56c7fb80918293dd9","5b00c4b56c7fb80918293dd7",...]
const users= await User.find({records:IDs})
IDS是对象ID的数组:
const ids = [
'4ed3ede8844f0f351100000c',
'4ed3f117a844e0471100000d',
'4ed3f18132f50c491100000e',
];
带回调:
User.find().where('_id').in(ids).exec(callback);
具有异步函数:
records = await User.find().where('_id').in(ids).exec();
不隶属于 StackOverflow