数组中mongoid更新内容 [英] mongoid update elements within array
问题描述
我使用mongoid 3.1使用Ruby 1.9.3,我试图更新数组内的值。我可以成功在MongoDB的CLI中执行以下命令,但似乎无法找到合适的解决方案/翻译mongoid。
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 }})
有关背景文件如下:
{
"_id" : ....,
"user_name" : "bob",
"activities: [
{
uuid: "1111111-xxxx-xxxx",
submitted: true,
},
{
uuid: "222222-xxxx-xxxx",
submitted: false,
},
{
uuid: "333333-xxxx-xxxx",
submitted: false,
}
]
}
的目标是真正基于uuid的值来更改提交。从我可以告诉,一切mongoid的更新的解决方案只与属性在处理文档的根,不能为$位置运营商的选择。
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.
任何帮助将是AP preciated。
Any help would be appreciated.
感谢您
推荐答案
看起来像我发现在GitHub上讨论的问题的答案。
Looks like I found the answer in a github issue discussion.
我的解决方案是:
User.elem_match(activities: { type: "redemption" }).elem_match(activities: { uuid: uuid }).update("$set" => {"activities.$.submitted" => true })
这篇关于数组中mongoid更新内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!