Re: [mongodb-user] Updating an element in an array using mongodb java driver

706 views
Skip to first unread message

Sam Millman

unread,
Oct 12, 2012, 11:18:24 AM10/12/12
to mongod...@googlegroups.com
You can check for the $exists of the qestions.questionID value and update only if it does not exist using the $push operator for the update as in the last post.

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

My collection is something like this,

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

I wanted to update questions array based on the users visit, suppose if user visits
question#2 then one more element should be added to the document like this,

{
    "_id" : ObjectId("5077e898e4b05e76f3b6f392"),
    "_class" : "com.citizenshipschool.domain.PracticeQuestion",
    "questions" : [
        {
            "questionID" : 1,
            "section" : "general"
        },
        {
            "questionID" : 2,
            "section" : "science"
        }
    ]
}

But if I use save() question #1 is getting updated with question #2 instead of
appending to it.

Also if user again visits a page which is already in the questions array for e.g. question #1
then the document shouldn't get updated.

Is there a way to achieve this using the mongodb java driver.

Thanks for any suggesstions

RK

--
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, 5:01:58 PM10/12/12
to mongod...@googlegroups.com
Cross-referencing this with a duplicate mailing list post that was also answered: https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/jXApSipw0HQ

Reply all
Reply to author
Forward
0 new messages