I have a document :
{
"_id" : ObjectId("550c00f81bcc15211016699b"),
"name" : "book3",
"author" : "mno",
"publisher" : "pub",
"testa" : [
{
"item1" : "item1",
"item2" : "item2"
}
]
}
All I want to do is add another item in testa like:
{
"_id" : ObjectId("550c00f81bcc15211016699b"),
"name" : "book3",
"author" : "mno",
"publisher" : "pub",
"testa" : [
{
"item1" : "item1",
"item2" : "item2",
"item3 : "item3"
}
]
}
I tried using
db.books.update(
{ "author":"mno" },
{ $addToSet: { testa : {"item3":"item3"} } }
)
this gives
{
"_id" : ObjectId("550c05261bcc15211016699c"),
"name" : "book3",
"author" : "mno",
"publisher" : "pub",
"testa" : [
{
"item1" : "item1",
"item2" : "item2"
},
{
"item3" : "item3"
}
]
}
and i tried
db.books.update(
{ "author":"mno" , "testa.item1" : "item1"},
{ $set : {"testa.0" : {"item3":"item3"}}},
{upsert : true / false}
)
this gave
{
"_id" : ObjectId("550c05fa1bcc15211016699d"),
"name" : "book3",
"author" : "mno",
"publisher" : "pub",
"testa" : [
{
"item3" : "item3"
}
]
}
Am I doing something wrong, I checked everywhere Insert an embedded document to a new field in mongodb document and Update or replace an embedded document in MongoDB collection
I tried wierd things... but.... Please help me get the query right
I also tried these using the C# driver like
WriteConcernResult res = booksColl.Update(query, Update.Set("testa.$.item1", "itemedited"),UpdateFlags.Upsert);