0

My schema structure looks something like this:

{
    "__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")
    },
    ...
    ]
}

I have 3 known variables: podId for podId, codeName for parameters.name, envType for parameters.type. Using them I want to update the object using podId, codeName, and envType, with a new variable paramValue that will contain the testValue value.

What I've tried

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

I'm trying to update the testValue where podId == podId, parameters.name == codeName, and parameters.type == envType, but the above did not update anything.

I also tried

db.pods.update({
    podId: podId,
    parameters: {
        name: codeName,
        type: envType
    }
}, {
    $push: {
        parameters: {
            testValue: paramValue
        }
    }
},
    function(err) {
        if(err) throw err;
    });

basically taking what worked to build the object when I only had to compare the podId, and adding the extra criteria; it didn't work this time.

edit: fixed schema type Number to String

sengakae
  • 39
  • 7
  • `parameters.type` is a string, are you sure you're matching on a string and not an integer? i.e is `envType` `1` or `"1"`? – BanksySan Apr 09 '16 at 17:46
  • Whoops I typed it in my question incorrectly. `env.type`, `parameters.type`, and `envType` are all strings. I've updated the post to reflect that. – sengakae Apr 09 '16 at 19:46

1 Answers1

0

Let these are variables that you have in mongo shell:

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

This query you can try:

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

Depending upon which testValue you want to update, you can pass index to that. Update

Following result i get after the update query:

db.test.find().pretty()

{ "_id" : ObjectId("570c850ab9477f18ac5297f9"), "__v" : 0, "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" : "updatedParamValue", "liveValue" : null, "_id" : ObjectId("5708423f897db8255aaaaa0d") }, { "name" : "sound.right", "type" : "1", "paramName" : "Right Sound Control", "paramType" : "booleanParameter", "testValue" : "updatedParamValue", "liveValue" : null, "_id" : ObjectId("5708423f897db8255aaaaa0d") } ] }

Thanks.

Himanshu Bhandari
  • 1,769
  • 2
  • 23
  • 37
  • There are several pods in this schema, and the parameters array contains upward to a thousand parameter objects. I want to only update the `testValue` of specific objects where the `podId`, `parameters.name`, and `parameters.type` match. – sengakae Apr 09 '16 at 18:36
  • So the query that I suggested isn't working? I have written the query keeping this thing in mind. – Himanshu Bhandari Apr 10 '16 at 05:02
  • Yeah the `testValue` are still null. – sengakae Apr 10 '16 at 19:32
  • Oh! Sorry, my mistake, I have updated the answer, please check. – Himanshu Bhandari Apr 11 '16 at 05:22
  • Still nothing. I think the problem is the selection criteria, because it returns nothing when I use it in a find. – sengakae Apr 11 '16 at 17:59
  • Impossible! May be you are using different name of the collection. I have inserted the document you gave fired the query and getting the result. Please post the steps that you are following if you want to get the solution. – Himanshu Bhandari Apr 12 '16 at 05:21
  • Well this is embarrassing, turns out I was using a different collection name in my script, so I was looking at the wrong collection in the shell. Thanks for your help. – sengakae Apr 12 '16 at 19:41
  • I thought the same :) Well if this solved your problem then please accept it as an answer. Thanks. – Himanshu Bhandari Apr 13 '16 at 05:14