0

This is my sample document in mongodb I want to update in value key

Original document

"_id" : ObjectId("54c76632a2abd01508508225"),
"del" : "N",
"value" : [
    {
        "1" : "A",
        "2" : "B",
        "fromDate" : ISODate("2015-02-13T20:59:28.947Z")
    }
]

I want to insert "3":"C" in value field

Expected document

"_id" : ObjectId("54c76632a2abd01508508225"),
"del" : "N",
"value" : [
    {
        "1" : "A",
        "2" : "B",
        "3" : "C",
        "fromDate" : ISODate("2015-02-13T20:59:28.947Z")
    }
]

I tried this query but it will created in wrongly

db.collection.update({ "_id" : ObjectId("54c76632a2abd01508508225")},
         {$push: { 
                    "value":{ "3":"C" } 
                  }
         }
         )

Thankyou

Arav KT
  • 301
  • 6
  • 18
  • I think the fact that `"value'` is an array and there is a single document inside it with numbered keys is more indicative of a pre-existing problem, rather than something you should be continuing. Why should this document be in this state? What is the purpose behind the numbered keys? – Neil Lunn Jun 27 '17 at 09:09
  • Possible duplicate of [MongoDB: Updating subdocument](https://stackoverflow.com/questions/5646798/mongodb-updating-subdocument) – Alex Blex Jun 27 '17 at 09:27
  • @Neil Lunn i put it in sample data only please help me – Arav KT Jun 27 '17 at 09:29
  • Help us help you by explaining what you mean. This is not a good data structure if it's a sample. If you have real data that is different, then you need to show that. Obscuring what you really need to do does not get you correct answers. – Neil Lunn Jun 27 '17 at 09:31
  • @Neil Lunn i know sir but our company used this structure for million of documents – Arav KT Jun 27 '17 at 09:32
  • Sorry but I don't answer questions like this. Obscuring things leads to too many follow up questions when you need to apply to your real case. It's a pretty silly company policy. Sort it out with your company. – Neil Lunn Jun 27 '17 at 09:34

2 Answers2

0
//Better You try Below Query
   db.collection.update(
        {
            "_id" : ObjectId("54c76632a2abd01508508225"),
            "value.1":"A",
            "value.2":"B",
        },
        {
            $set:{
                "value.$.3": "C"
            }
        },function(err,Status){
            if(!err){
                console.log("Update Successfully")
            }
        }
    )

The above query update only when Value.1 is "A" and Value.2 is "B"

Dear i suggest you to not to start your field name with numeric

Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
  • i got error showing >WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 16836, "errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: value.$.3" } }) – Arav KT Jun 28 '17 at 11:36
  • @Arav you take start your field name with character not numeric – Ratan Uday Kumar Jun 28 '17 at 11:45
0

To use $push field type should be an array, when you try with that query it will push to value but not value.0; here you are trying to push to object inside, that will not work.

db.collection.update(
{ 
  _id : ObjectId("54c76632a2abd01508508225"),
    "value.1" : "A",
    "value.2" : "B"
},
{ 
  $set: {
          "value.$" : {
              "1" : "A",
              "2" : "B",
              "3" : "C"
              }
  }
}
);

With this query you can replace whole object inside value array or I strongly recommend to rethink the schema

Naga Penmetsa
  • 384
  • 5
  • 16