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

Showing 1-2 of 2 messages
Re: [mongodb-user] Updating the elements in an array Sammaye 10/12/12 8:10 AM
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

Re: [mongodb-user] Updating the elements in an array Jeremy Mikola 10/12/12 1:38 PM

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.