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

6
  • 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? Commented Jun 27, 2017 at 9:09
  • Possible duplicate of MongoDB: Updating subdocument Commented Jun 27, 2017 at 9:27
  • @Neil Lunn i put it in sample data only please help me Commented Jun 27, 2017 at 9: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. Commented Jun 27, 2017 at 9:31
  • @Neil Lunn i know sir but our company used this structure for million of documents Commented Jun 27, 2017 at 9:32

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 you take start your field name with character not numeric
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

Comments

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.