2

My problem is I want to push data on to a specified position in an array in a MongoDb Document, but when I push the new data it doesn't add it to the right position.

When run this update statement:

collection.update({ "schraenke.name": "Schrank1" }, {
                    $push:
                    {
                        "schraenke": [{
                            "masterbricks": {
                                "sensor1": { value: "test" }
                            }
                        }]
                    }
                });`

The result from the above update is the document:

{
    "_id": "55599876095c6bac18209dfa",
    "name": "Serverraum1",
    "schraenke": [
      {
        "name": "Schrank1",
        "nummer": "1",
        "reihe": "1",
        "masterbricks": {
          "name": "Mastrebrick2.0",
          "uid": "6dKiRt",
          "he": "1-20",
          "sensor1": {
            "name": "Temperatur",
            "uid": "qvf"
          }
        }
      },
      [
        {
          "masterbricks": {
            "sensor1": {
              "value": "test"
            }
          }
        }
      ],

    ]
  }

The value field should be added to "sensor1", but it adds a new array.

My expected result is:

{ 
    "_id": "55599876095c6bac18209dfa", 
    "name": "Serverraum1", 
    "schraenke": [ 
        { 
           "name": "Schrank1", 
           "nummer": "1", 
           "reihe": "1", 
           "masterbricks": { 
               "name": "Mastrebrick2.0", 
               "uid": "6dKiRt", 
               "he": "1-20", 
               "sensor1": { 
                   "name": "Temperatur", 
                   "uid": "qvf", 
                   "value" : "test" 
               } 
           } 
       }, 
   ] 
}

What am I doing wrong?

6
  • What is your expected result? Should your value field be an array or just a single value? Commented May 18, 2015 at 8:22
  • My expecting result is: { "_id": "55599876095c6bac18209dfa", "name": "Serverraum1", "schraenke": [ { "name": "Schrank1", "nummer": "1", "reihe": "1", "masterbricks": { "name": "Mastrebrick2.0", "uid": "6dKiRt", "he": "1-20", "sensor1": { "name": "Temperatur", "uid": "qvf", "value" : "test" } } }, ] } Commented May 18, 2015 at 8:27
  • That the value is add to the sensor1 Commented May 18, 2015 at 8:28
  • 1
    @MarcoK chridam's answer will work for you. Commented May 18, 2015 at 8:31
  • 1
    so you mean the sensor document must have a value filed which is an array of measured values? Commented May 18, 2015 at 8:58

3 Answers 3

4

Instead of using $push you need to apply the $set operator together with the $ positional operator in your update to push the new document into the right position. The $ positional operator will identify the correct element in the array to update without explicitly specifying the position of the element in the array, thus your final update statement should look like:

db.collection.update({ "schraenke.name": "Schrank1" }, 
    {
        "$set": {
            "schraenke.$.masterbricks.sensor1.value": "test" 
        }
    }
)

Result:

/* 0 */
{
    "_id" : "55599876095c6bac18209dfa",
    "name" : "Serverraum1",
    "schraenke" : [ 
        {
            "name" : "Schrank1",
            "nummer" : "1",
            "reihe" : "1",
            "masterbricks" : {
                "name" : "Mastrebrick2.0",
                "uid" : "6dKiRt",
                "he" : "1-20",
                "sensor1" : {
                    "name" : "Temperatur",
                    "uid" : "qvf",
                    "value" : "test"
                }
            }
        }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

0

$push adds the array field with the value as its element.

You should use $Set

collection.update({ "schraenke.name": "Schrank1" }, {
                    $set:
                    {
                        "schraenke": [{
                            "masterbricks": {
                                "sensor1": { value: "test" }
                            }
                        }]
                    }
                });

2 Comments

Your Example overwrite the existing data, dosent add anything to it.
Check @chridam's answer. It should be just overwrite "value" field not the sensor1
0

If you use $push then "value" : ["test"] array will added into sensor1 using following update query :

db.collectionName.update({"schraenke.name":"Schrank1"},
  {"$push":{"schraenke.$.masterbricks.sensor1.value":"test"}})

Instead of push use $set as mentioned above chridam and used upsert true in set.

3 Comments

Yes a want to add a new value and the value should not be overwritten, it should create a new value array
My new query is: collection.update({ "schraenke.name": "Schrank1" }, { $set: { "schraenke.$.masterbricks.sensor1.value": "Test" } }, { upsert: true }); But its overwriting the existing value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.