Mongodb找到如果字段存在使用其中


Mongodb find if field exists using a wherein

基本上可以使用$in查找基于子文档的文档。例如,我有:

array(
'item.1',
'item.2',
'item.3
)

返回的文档包含:

{
    item {
        1: {
        }
    }
}
{
    item {
        2: {
        }
    }
}
{
    item {
        3: {
        }
    }
}

我知道如果我有一个,我可以用db.inventory.find( { qty: { $exists: true, $nin: [ 5, 15 ] } } ),但我怎么用$in呢?

您可以使用这种形式的查询:

db.collection.find ( { $or : [
          {"item.1":{$exists:true}},
          {"item.2":{$exists:true}},
          {"item.3":{$exists:true}}
] } );

这将返回任何包含一个或多个"item "的文档。