2

I am using mongoid 3.1 with Ruby 1.9.3 and I am trying to update a value within an array. I can successfully perform the following command in mongodb's CLI, but can't seem to find the appropriate solution/translation for mongoid.

user.update( { activities: { $elemMatch: { uuid: "1111111-xxxx-xxxx" }}}, { $set: { 'activities.$.submitted': true }})

For context the document looks like:

{
   "_id" : ....,
   "user_name" : "bob",
   "activities: [ 
       {
          uuid: "1111111-xxxx-xxxx",
          submitted: true,
       },
       {
          uuid: "222222-xxxx-xxxx",
          submitted: false,
       },
       {
          uuid: "333333-xxxx-xxxx",
          submitted: false,
       }
   ]
}

The goal is to change submitted to true based on the uuid value. From what I can tell, all of the "updating" solutions in mongoid only deal with the attributes at the root of the document and can't have options for the $ positional operator.

Any help would be appreciated.

Thank you

1
  • 1
    Looks like I found the answer in a github issue discussion. The solution for me was: User.elem_match(activities: { type: "redemption" }).elem_match(activities: { uuid: uuid }).update("$set" => {"activities.$.submitted" => true }) Commented Aug 20, 2013 at 23:14

1 Answer 1

2

Looks like I found the answer in a github issue discussion.

The solution for me was:

User.elem_match(activities: { type: "redemption" }).elem_match(activities: { uuid: uuid }).update("$set" => {"activities.$.submitted" => true })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.