Insert data into nested array in mongodb

Possible duplicate:
MongoDB update fields in a nested array

I have data like:

{ 
    "_id" : ObjectId("4f855061dd53351011000b42"), 
    "act_mgr" : [{ "sales" : {"agent" : ["rohan@walkover.in" ],  "last_interacted" : "rohan@walkover.in" } } ],
    "email" : "aman@asasas.com", "name" : "Aman",
    "sales" : [{"sno" : 1,  "message" : "description","status" : "open"},{"sno" : 12,"message" : "assad","status" :"open"}]
}

I want to add a new agent and update last_interacted in act_mgr: selling something like this

"act_mgr" : [{ "sales" : {"agent" : ["rohan@walkover.in","abc@walkover.in" ],
 "last_interacted" : "abc@walkover.in" } } ]

Also, if I add a new act_mgr as a developer, it will look like

 "act_mgr" : [{ "sales" : {"agent" : ["rohan@walkover.in","abc@walkover.in" ],  "last_interacted" : "abc@walkover.in" } },
 { "developer" : {"agent" : ["newdeveloper@walkover.in" ],  "last_interacted" : "newdeveloper@walkover.in" } } ]

I do not know how to add these fields

+3
source share
1 answer

You can update the embedded sales document inside the act_mgr array with the following update statement:

> db.sales.update({"act_mgr.sales.last_interacted":"rohan@walkover.in"}, {$push:{"act_mgr.$.sales.agent":"abc@walkover.in"}, $set:{"act_mgr.$.sales.last_interacted":"abc@walkover.in"}})
> db.sales.find().pretty()
{
    "_id" : ObjectId("4f855061dd53351011000b42"),
    "act_mgr" : [
        {
            "sales" : {
                "agent" : [
                    "rohan@walkover.in",
                    "abc@walkover.in"
                ],
                "last_interacted" : "abc@walkover.in"
            }
        }
    ],
    "email" : "aman@asasas.com",
    "name" : "Aman",
    "sales" : [
        {
            "sno" : 1,
            "message" : "description",
            "status" : "open"
        },
        {
            "sno" : 12,
            "message" : "assad",
            "status" : "open"
        }
    ]
}
> 

You can add an embedded document containing information about the "developer" into the array as follows:

> db.sales.update({"_id" : ObjectId("4f855061dd53351011000b42")}, {$push:{"act_mgr":{ "developer" : {"agent" : ["newdeveloper@walkover.in" ],  "last_interacted" : "newdeveloper@walkover.in" } }}})
> db.sales.find().pretty()
{
    "_id" : ObjectId("4f855061dd53351011000b42"),
    "act_mgr" : [
        {
            "sales" : {
                "agent" : [
                    "rohan@walkover.in",
                    "abc@walkover.in"
                ],
                "last_interacted" : "abc@walkover.in"
            }
        },
        {
            "developer" : {
                "agent" : [
                    "newdeveloper@walkover.in"
                ],
                "last_interacted" : "newdeveloper@walkover.in"
            }
        }
    ],
    "email" : "aman@asasas.com",
    "name" : "Aman",
    "sales" : [
        {
            "sno" : 1,
            "message" : "description",
            "status" : "open"
        },
        {
            "sno" : 12,
            "message" : "assad",
            "status" : "open"
        }
    ]
}
> 

$push $set "": http://www.mongodb.org/display/DOCS/Updating

Mongo db " ( )" http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29

"$" " $positional" "".
http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator

: , . . , "sno", "message" "status"

"act_mgr" ; "", "". , , :

"act_mgr" : [
    {
        "title" : "sales",
        "agent" : [
            "rohan@walkover.in",
            "abc@walkover.in"
        ],
        "last_interacted" : "abc@walkover.in"
    },
    {
        "title": "developer",
        "agent" : [
            "newdeveloper@walkover.in"
        ],
        "last_interacted" : "newdeveloper@walkover.in"
    }
]

, "title", "agent" "last_interacted".

.

> db.sales.update({"act_mgr.title":"sales"}, {$push:{"act_mgr.$.agent":"abc@walkover.in"}, $set:{"act_mgr.$.last_interacted":"abc@walkover.in"}})

, , , . !

+5

All Articles