mongo找到多个数组对

时间:2016-02-09 19:44:22

标签: mongodb

如果我有一对像这样的对:

[
  {foo: 'a', bar: 'b'},
  {foo: 'b', bar: 'c'},
  {foo: 'a', bar: 'd'},
  {foo: 'b', bar: 'b'},
]

我想在一个集合中找到与这些对完全匹配的文档,我该怎么做?

我看过$ in,$ all,$ elemMatch运营商,但他们似乎没有做我想做的事。

我可以单独进行查询:

db.baz.find({foo: 'a', bar: 'b'})
db.baz.find({foo: 'b', bar: 'c'})
db.baz.find({foo: 'a', bar: 'd'})
db.baz.find({foo: 'b', bar: 'b'})

但我想做的事情是这样的:

db.baz.find([
  {foo: 'a', bar: 'b'},
  {foo: 'b', bar: 'c'},
  {foo: 'a', bar: 'd'},
  {foo: 'b', bar: 'b'},
]);

1 个答案:

答案 0 :(得分:1)

尝试$或语法:

db.baz.find({
    $or: [
        { foo: 'a', bar: 'b' },
        { foo: 'b', bar: 'c' },
        { foo: 'a', bar: 'd' },
        { foo: 'b', bar: 'b' },
    ]
})
相关问题