最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - MongoDB update nested array - Stack Overflow

programmeradmin1浏览0评论

I have a simple structure in mongodb, with nested array. How can I update searched value? I've seen examples using numbers something like this:

invited.0.used: true

but this is not what I'm searching for because I don't know where in my list is this element so how could I update array used to true where key is 84026702? What if I have 100 arrays in invited how to update where key is 43938432?

{
    "_id" : ObjectId("4fed972f61d69aa004000000"),
    "name" : "mezo",
    "invited" : [
            {
                    "key" : 40928710,
                    "used" : false
            },
            {
                    "key" : 84026702,
                    "used" : false
            }
    ]
}

I have a simple structure in mongodb, with nested array. How can I update searched value? I've seen examples using numbers something like this:

invited.0.used: true

but this is not what I'm searching for because I don't know where in my list is this element so how could I update array used to true where key is 84026702? What if I have 100 arrays in invited how to update where key is 43938432?

{
    "_id" : ObjectId("4fed972f61d69aa004000000"),
    "name" : "mezo",
    "invited" : [
            {
                    "key" : 40928710,
                    "used" : false
            },
            {
                    "key" : 84026702,
                    "used" : false
            }
    ]
}
Share Improve this question asked Jun 29, 2012 at 12:09 user50992user50992 2013 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6
update({ invited.key : 84026702 }, { invited.$.used : true });

This basically does what you wanna and should work nicely. Look into positional operators in mongodb: http://www.mongodb/display/DOCS/Updating#Updating-The%24positionaloperator

Or in PHP (as your question is tagged) you can do:

$mongo->collection->update(array('invited.key' => 84026702), array('invited.$.used' => true));
发布评论

评论列表(0)

  1. 暂无评论