假设我有一个名为User
. 我有一个带有对象 ID 的数组。
我想获取与我拥有的 Id 数组“相交”的所有用户记录。
User.find({ records with IDS IN [3225, 623423, 6645345] }, function....
这是使用 $in 运算符的猫鼬方式。
User.find()
.where('fb.id')
.in([3225, 623423, 6645345])
.exec(function (err, records) {
//make magic happen
});
我发现点符号对于查询子文档非常方便。
您需要使用 $in 运算符 >
https://docs.mongodb.com/manual/reference/operator/query/in/#op._S_in
例如:
Users.find( { "fb" : { id: { $in : arrayOfIds } } }, callback );
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();