41

假设我有一个名为User. 我有一个带有对象 ID 的数组。

我想获取与我拥有的 Id 数组“相交”的所有用户记录。

User.find({ records with IDS IN [3225, 623423, 6645345] }, function....
4

5 回答 5

84

这是使用 $in 运算符的猫鼬方式。

User.find()
  .where('fb.id')
  .in([3225, 623423, 6645345])
  .exec(function (err, records) {
    //make magic happen
  });

我发现点符号对于查询子文档非常方便。

http://mongoosejs.com/docs/queries.html

于 2012-11-13T02:56:48.023 回答
44

您需要使用 $in 运算符 >

https://docs.mongodb.com/manual/reference/operator/query/in/#op._S_in

例如:

Users.find( { "fb" : { id: { $in : arrayOfIds } } }, callback );
于 2011-04-28T17:02:36.763 回答
8
User.where({ records: { $in: [3225, 623423, 6645345] } }, function ...

更多信息在这里:http ://docs.mongodb.org/manual/reference/operator/query/

于 2014-01-11T09:51:08.540 回答
0

对我来说,这样工作

IDs=["5b00c4b56c7fb80918293dd9","5b00c4b56c7fb80918293dd7",...]
const users= await User.find({records:IDs}) 
于 2018-05-20T01:08:35.523 回答
0

Ids 是对象 ID 的数组:

const ids =  [
    '4ed3ede8844f0f351100000c',
    '4ed3f117a844e0471100000d', 
    '4ed3f18132f50c491100000e',
];

带回调:

User.find().where('_id').in(ids).exec(callback);

使用异步功能:

records = await User.find().where('_id').in(ids).exec();
于 2019-08-24T10:22:55.087 回答