Details
-
Improvement
-
Resolution: Done
-
Major - P3
-
None
-
None
-
osx 10.9, mongodb 2.4.6
*Location*: http://docs.mongodb.org/manual/reference/operator/update/positional/
*User-Agent*: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
*Referrer*: http://docs.mongodb.org/manual/reference/operator/update-array/
*Screen Resolution*: 1920 x 1200
*repo*: docs
*source*: reference/operator/update/positional
osx 10.9, mongodb 2.4.6 *Location*: http://docs.mongodb.org/manual/reference/operator/update/positional/ *User-Agent*: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36 *Referrer*: http://docs.mongodb.org/manual/reference/operator/update-array/ *Screen Resolution*: 1920 x 1200 *repo*: docs *source*: reference/operator/update/positional
Description
Hi. Let use next doc for example:
db.test.save({
|
_id: 1,
|
categories: [
|
{parent_id: 1001, _id: 1, name: 'one'}, |
{parent_id: 1001, _id: 2, name: 'two'}, |
{parent_id: 1001, _id: 3, name: 'three'}, |
]
|
})
|
When i'm doing:
db.test.update({_id: 1, 'categories._id': 2, 'categories.parent_id': 1001}, {$set: {'categories.$.name': 'two - UPDATED'}}) |
expecting to see second sub-document's name change. However, this query will update first sub-doc:
db.test.find({_id: 1}).pretty()
|
{
|
"_id" : 1, |
"categories" : [ |
{
|
"_id" : 1, |
"name" : "two - UPDATED", |
"parent_id" : 1001 |
},
|
{
|
"parent_id" : 1001, |
"_id" : 2, |
"name" : "two" |
},
|
{
|
"parent_id" : 1001, |
"_id" : 3, |
"name" : "three" |
}
|
]
|
}
|
To get expected results I'll have to move 'categories._id' parameter in query to the end:
db.test.update({_id: 1, 'categories.parent_id': 1001, 'categories._id': 2}, {$set: {'categories.$.name': 'two - UPDATED'}}) |