mk2015
mk2015

Reputation: 213

How to push Data into existing Document in MongoDB?

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?

Upvotes: 2

Views: 1644

Answers (3)

Neo-coder
Neo-coder

Reputation: 7840

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.

Upvotes: 0

chridam
chridam

Reputation: 103475

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"
                }
            }
        }
    ]
}

Upvotes: 4

Erkan Demirel
Erkan Demirel

Reputation: 4382

$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" }
                            }
                        }]
                    }
                });

Upvotes: 0

Related Questions