根据值数组匹配Meteor集合

时间:2017-04-19 04:54:07

标签: mongodb meteor collections

假设我有一个ID列表,我希望通过以下方式过滤Collection:

const ids = [1, 2, 3, 4];

如何过滤集合以匹配这些ID?这样的事情不起作用:

return Coll.fetch({_id: {$all: ids}});

2 个答案:

答案 0 :(得分:3)

这将有效:

return Collection.find({_id: {$in: ids}}).fetch();

答案 1 :(得分:0)

以下是我如何做到这一点:

function hasAllIds(collections, ids) {
  for (let i = 0; i < collections.length; i++) {
    let count = collections[i].find({
      _id: {
        $in: ids
      }
    }).count();
    if (count === ids.length) {
      return collections[i];
    }
  }
  return null;
}

const colls = [Meteor.users, Games]; //etc.
const ids = [1, 2, 3];
const coll = hasAllIds(colls, ids);
if (coll) {
  coll.find(); //or whatever
}
相关问题