mongodb中的$ elementMatch查询

时间:2016-09-17 14:01:23

标签: mongodb mongodb-query

我需要在[" 800"," 200"," 400"]

中找到具有product.typeCode的元素。
{
    "_id": "stdcl13@ml.com",
    "_class": "com.mongodb.BasicDBObject",
    "accounts": [{
        "number": "96398-00910620286__DISABILITY",
        "product": {
            "typeCode": "400",
            "nameCode": "401"
        },
        "dependents": [],
        "_id": "stdcl13@ml.com96398-00910620286__DISABILITYDSB"
    }, {
        "number": "96398-00910620286__LIFECNV",
        "product": {
            "typeCode": "300",
            "nameCode": "LIFECNV"
        },
        "dependents": [],
        "_id": "stdcl13@ss.com"
    }]
}

我写了这个查询,但它没有返回结果

find( {
    accounts: {
        $elemMatch: {
            product: {
                 typeCode: {
                    $in: ["400"]
                 }
            }
        }
    }
})

1 个答案:

答案 0 :(得分:1)

请尝试以下查询。您可以在IN子句中添加其他值(800,200,400)。

db.collection.find({ "accounts.product.typeCode" : {
                    $in: ["400"]
                 }
            }
    );

包含所有三个值: -

db.productlist.find({ "accounts.product.typeCode" : {
                    $in: ["400", "800", "200"]
                 }
            }
    );