MongoDB:查找包含所有字段和值(除了_id)等于给定文档的文档?

时间:2016-02-23 15:46:19

标签: mongodb filter find field database

有没有办法搜索所有字段和值(除了_id)包含在给定文档字段中的文档?

示例:

{_id:..., "field1" : 2 , "field2" : 3}
{_id:..., "field1" : 2 , "field2" : 4}
{_id:..., "field1" : 2 , "field3" : 1}

要放入查询的文档:

{"field1" : 2 , "field2" : 4, "field3" : 1}

预期结果:

{_id:..., "field1" : 2 , "field2" : 4}
{_id:..., "field1" : 2 , "field3" : 1}

2 个答案:

答案 0 :(得分:0)

试试这个:

db.numbers.find({ $or: [ {"field1" : 2 , "field2" : 4}, {"field1" : 2 , "field3" : 1} ]})

答案 1 :(得分:0)

我认为你需要的是:

db.yourdb.find({$and : [ {$or : [{"field1" :2}, {"field1" : {$exists : false}}]},  {$or : [{"field2" :4}, {"field2": {$exists : false}}]},{$or : [{"field3" :q}, {"field3": {$exists : false}}]}]})