-1
{    

    "_id" : ObjectId("5822d0c92f136292e5e4a0cc"),  
    "Age" : "five",   
    "details" : [   
                 {  
                    "Name" : "abc"  
                }  
            ]      
}

how to add new value in existing array.As i shown below;
Desire Output:

{    

    "_id" : ObjectId("5822d0c92f136292e5e4a0cc"),  
    "Age" : "five",   
    "details" : [   
                 {  
                    "Name" : "abc"  
                    "Name" : "xyz"  
                }  
            ]      
}

And with this code, i am getting this output:-

db.c1.update({"_id" : ObjectId("5822d0c92f136292e5e4a0cc")},
{$push:{"details":{"Name":"xyz"}}})

output:-

 {
"_id" : ObjectId("5822d0c92f136292e5e4a0cc"),
"Age" : "five",
"details" : [
    {
        "Name" : "xyz"
    },
    {
        "Name" : "abc"
    }
  ]
 }

Please help me out.Thanks in advance.

TB.M
  • 363
  • 3
  • 8
  • 26
  • 2
    Your desired output is not making any sense. you can't have an **Object** that contains more than 1 similar key. please refer this question http://stackoverflow.com/questions/13987365/how-to-insert-an-element-to-mongodb-internal-list – molaga Nov 09 '16 at 08:00

1 Answers1

0

Try this but cannot give same Name field Here i have put name n to change Name field

db.c1.find({}).forEach(function(doc){
    doc.insertupdatesession[0].name = "abc"
    db.getCollection('cityMaster').save(doc);
    })
Parshuram Kalvikatte
  • 1,616
  • 4
  • 20
  • 40