admin管理员组文章数量:1022874
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
1 Answer
Reset to default 6update({ 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));
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
1 Answer
Reset to default 6update({ 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));
本文标签: phpMongoDB update nested arrayStack Overflow
版权声明:本文标题:php - MongoDB update nested array - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745575298a2156981.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论