Re: [mongodb-user] Updating the elements in an array

178 views
Skip to first unread message

Sam Millman

unread,
Oct 12, 2012, 11:10:58 AM10/12/12
to mongod...@googlegroups.com
Those two documents are the same.

However I am guessing you wish to push a new subdocument to the questions field. You can use $push for this: http://www.mongodb.org/display/DOCS/Updating#Updating-%24push

On 12 October 2012 15:55, RK <ero...@gmail.com> wrote:
Hi,

My collection is something like this,

{
    "_id" : ObjectId("5077e898e4b05e76f3b6f392"),
   
    "questions" : [
        {
            "questionID" : 1,
             "section" : "general"
        }
       
    ]
}

I wanted to add elements to the questions array based on the users visit, For e.g if the user visits question # 2 then the document should be updated something like this,

{
    "_id" : ObjectId("5077e898e4b05e76f3b6f392"),
   
    "questions" : [
        {
            "questionID" : 1,
             "section" : "general"
        },

       
    ]
}



--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Jeremy Mikola

unread,
Oct 12, 2012, 4:38:31 PM10/12/12
to mongod...@googlegroups.com

On Friday, October 12, 2012 11:11:15 AM UTC-4, Sammaye wrote:
Those two documents are the same.

However I am guessing you wish to push a new subdocument to the questions field. You can use $push for this: http://www.mongodb.org/display/DOCS/Updating#Updating-%24push


Since we're dealing with visits you may want to only add an object to the array if it's not already present. In that case, the $addToSet operator might be desirable.
Reply all
Reply to author
Forward
0 new messages