我在mongodb中有一个简单的结构,带有嵌套数组。如何更新搜索值? 我见过类似这样的数字的例子:
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但这不是我要搜索的内容,因为我不知道我的列表中的这个元素在哪里所以如何如果密钥是84026702,我可以更新用于true的数组吗?如果我有100个阵列被邀请如何更新密钥是43938432怎么办?
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 } ] }推荐答案
update({ invited.key : 84026702 }, { invited.$.used : true });
这基本上是你想要的,应该很好地工作。查看mongodb中的位置运算符: www.mongodb/display/DOCS/Updating #更新 - %24positionaloperator
This basically does what you wanna and should work nicely. Look into positional operators in mongodb: www.mongodb/display/DOCS/Updating#Updating-The%24positionaloperator
或者在PHP中(如你的问题被标记)你可以这样做:
Or in PHP (as your question is tagged) you can do:
$mongo->collection->update(array('invited.key' => 84026702), array('invited.$.used' => true));更多推荐
MongoDB更新嵌套数组
发布评论