How to add a sub document to sub document array in MongoDB -


i have collection in mogodb this:

{ "_id" : objectid("5393006538efaae30ec2d458"), "username" : "shiva", "userunderapikey" : 123456, "groups" : [     {         "groupname" : "default",         "groupmembers" : [ ]     } ] } 

i want add new group in groups array sub document, below

 { "_id" : objectid("5393006538efaae30ec2d458"), "username" : "shiva", "userunderapikey" : 123456, "groups" : [     {         "groupname" : "default",         "groupmembers" : [ ]     },      {         "groupname" : "family",         "groupmembers" : [ ]     } ] } 

how insert new sub document in array of sub documents.any appriciated

to add new member array use $push would:

db.collection.update(     { "_id": objectid("5393006538efaae30ec2d458") },     {          "$push": {             "groups": {                 "groupname" : "family",                 "groupmembers" : [ ]             }         }     } ) 

if wanted add members array member contains need match element want add to:

db.collection.update(     {          "_id": objectid("5393006538efaae30ec2d458"),         "groups.groupname" : "family",     },     {          "$push": {             "groups.$.groupmembers" : "bill"         }     } ) 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -