You can update the embedded "sales" document inside of the "act_mgr" array with the following update statement:
> db.sales.update({"act_mgr.sales.last_interacted":"[email protected]"}, {$push:{"act_mgr.$.sales.agent":"[email protected]"}, $set:{"act_mgr.$.sales.last_interacted":"[email protected]"}})
> db.sales.find().pretty()
{
"_id" : ObjectId("4f855061dd53351011000b42"),
"act_mgr" : [
{
"sales" : {
"agent" : [
"[email protected]",
"[email protected]"
],
"last_interacted" : "[email protected]"
}
}
],
"email" : "[email protected]",
"name" : "Aman",
"sales" : [
{
"sno" : 1,
"message" : "description",
"status" : "open"
},
{
"sno" : 12,
"message" : "assad",
"status" : "open"
}
]
}
>
You can add the embedded document containing the "developer" information to the array like so:
> db.sales.update({"_id" : ObjectId("4f855061dd53351011000b42")}, {$push:{"act_mgr":{ "developer" : {"agent" : ["[email protected]" ], "last_interacted" : "[email protected]" } }}})
> db.sales.find().pretty()
{
"_id" : ObjectId("4f855061dd53351011000b42"),
"act_mgr" : [
{
"sales" : {
"agent" : [
"[email protected]",
"[email protected]"
],
"last_interacted" : "[email protected]"
}
},
{
"developer" : {
"agent" : [
"[email protected]"
],
"last_interacted" : "[email protected]"
}
}
],
"email" : "[email protected]",
"name" : "Aman",
"sales" : [
{
"sno" : 1,
"message" : "description",
"status" : "open"
},
{
"sno" : 12,
"message" : "assad",
"status" : "open"
}
]
}
>
The documentation on the $push and $set modifiers may be found in the "Updating" documentation:
http://www.mongodb.org/display/DOCS/Updating
More information on creating and updating embedded documents with Mongo db may be found in the documentation titled "Dot Notation (Reaching into Objects)"
http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29
Information on updating embedded documents using the "$" positional operator may be found in the "The $ positional operator" section of the "Updating" documentation.
http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator
A word of caution: It is generally more common to have embedded documents all match the same structure, so that individual embedded documents may be referenced more easily. Your "sales" array is a good example of this; each embedded document contains the same keys, "sno", "message", and"status"
However, the embedded documents inside your "act_mgr" array contain different keys; the first contains "sales", and the second contains "developer". Instead, maybe consider the following structure:
"act_mgr" : [
{
"title" : "sales",
"agent" : [
"[email protected]",
"[email protected]"
],
"last_interacted" : "[email protected]"
},
{
"title": "developer",
"agent" : [
"[email protected]"
],
"last_interacted" : "[email protected]"
}
]
Now, each embedded documents contain the same keys, "title", "agent", and "last_interacted".
You could update sub-documents with the following command.
> db.sales.update({"act_mgr.title":"sales"}, {$push:{"act_mgr.$.agent":"[email protected]"}, $set:{"act_mgr.$.last_interacted":"[email protected]"}})
Hopefully this will allow you to make the updates that you need to, and perhaps give you some food for thought regarding schema design. Good luck!