如何使用多个选择条件更新mongodb对象

时间:2016-04-09 00:21:20

标签: mongodb mongo-shell

我的架构结构如下所示:

{
    "__v":0,
    "_id":ObjectId("5708423e897db8255aaaa9dd"),
    "podId":169400000005,
    "env":[
    {
        "type":"1",
        "id":3852000000035,
        "_id":ObjectId("5708423e897db8255aaaa9de")
    },
    {
        "type":"2",
        "id":3852000000040,
        "_id":ObjectId("5708423e897db8255aaaa9df")
    }
    ],
    "name":"Test Build",
    "parameters":[
    {
        "name":"sound.left",
        "type":"1",
        "paramName":"Left Sound Control",
        "paramType":"booleanParameter",
        "testValue":null,
        "liveValue":null,
        "_id":ObjectId("5708423f897db8255aaaaa0d")
    },
    {
        "name":"sound.right",
        "type":"1",
        "paramName":"Right Sound Control",
        "paramType":"booleanParameter",
        "testValue":null,
        "liveValue":null,
        "_id":ObjectId("5708423f897db8255aaaaa0d")
    },
    ...
    ]
}

我有3个已知变量:podIdpodIdcodeNameparameters.nameenvTypeparameters.type。使用它们我想使用podIdcodeNameenvType更新对象,并使用包含paramValue值的新变量testValue

我尝试了什么

db.pods.update({
    podId: podId,
    parameters: {
        $elemMatch: {
            name: codeName,
            type: envType
        }
    }
}, {
    $set: {
        'parameters.$.testValue': paramValue
    }
});

我正在尝试更新testValue podId == podIdparameters.name == codeNameparameters.type == envType,但上述内容未更新。

我也试过

db.pods.update({
    podId: podId,
    parameters: {
        name: codeName,
        type: envType
    }
}, {
    $push: {
        parameters: {
            testValue: paramValue
        }
    }
},
    function(err) {
        if(err) throw err;
    });
当我只需要比较podId并添加额外的标准时,

基本上采用了构建对象的方法;这次没用。

编辑:修复架构类型Number to String

1 个答案:

答案 0 :(得分:0)

让这些是你在mongo shell中的变量:

> var podId = 169400000005
> var codeName = "sound.right"
> var envType = "1"
> var paramValue = "updatedParamValue"

您可以尝试此查询:

db.test.update({"podId":podId, "parameters.name" : codeName, "parameters.type" : envType}, {$set : {"parameters.0.testValue" : paramValue,"parameters.1.testValue" : paramValue}})

根据您要更新的testValue,您可以将索引传递给它。 的更新

以下结果我得到更新后的查询:

db.test.find().pretty()
  

{           " _id" :ObjectId(" 570c850ab9477f18ac5297f9"),           " __ V" :0,           " PODID" :169400000005,           " ENV" :[                   {                           "类型" :" 1",                           " ID" :3852000000035,                           " _id" :ObjectId(" 5708423e897db8255aaaa9de")                   },                   {                           "类型" :" 2",                           " ID" :3852000000040,                           " _id" :ObjectId(" 5708423e897db8255aaaa9df")                   }           ]           "名称" :"测试构建",           "参数" :[                   {                           "名称" :" sound.left",                           "类型" :" 1",                           " PARAMNAME" :"左声控",                           " paramType" :" booleanParameter",                           "测试值" :" updatedParamValue ",                           " liveValue" : 空值,                           " _id" :ObjectId(" 5708423f897db8255aaaaa0d")                   },                   {                           "名称" :" sound.right",                           "类型" :" 1",                           " PARAMNAME" :"正确的声音控制",                           " paramType" :" booleanParameter",                           "测试值" :" updatedParamValue ",                           " liveValue" : 空值,                           " _id" :ObjectId(" 5708423f897db8255aaaaa0d")                   }           ]   }

感谢。