Mongo $ pullAll对象数组

时间:2018-02-22 16:40:17

标签: java arrays mongodb mongodb-query

目前我在mongo中有这样的数据结构:

{
    "_id": "field00048",
    "colorValues": [
        {
            "stateField": "Open1",
            "color": "purple"
        },
        {
            "stateField": "Open2",
            "color": "blue"
        }
    ]
}

我正在使用mongo shell创建一个查询,使用$ pullAll通过匹配stateField字段来删除colorValues数组中的两个对象。我已经尝试过这些查询,但它们都没有删除它们匹配的记录。

db.admin.fields.update({_id: "field00048"}, {$pullAll: {stateField: ["Open1", "Open2"]}});

db.admin.fields.update({_id: "field00048"}, {$pullAll: {colorValues: ["Open1", "Open2"]}});

db.admin.fields.update({_id: "field00048"}, {$pullAll: {colorValues: [{stateField:"Open1"}, {stateField:"Open2"}]}});

db.admin.fields.update({_id: "field00048"}, {$pullAll: {colorValues.stateField: ["Open1", "Open2"]}});

db.admin.fields.update({_id: "field00048"}, {$pullAll: {colorValues: stateField:{["Open1", "Open2"]}}});

有什么建议吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

解决此问题的查询是:

db.admin.fields.update({_id: "field00048"}, {$pull: {colorValues:{stateField:{$in: ["Open1", "Open2"]}}}})