Tuesday, 20 August 2013

mongoid update elements within array

mongoid update elements within array

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

No comments:

Post a Comment