Skip to content

Instantly share code, notes, and snippets.

@ismell
Created August 8, 2014 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ismell/9ae5a9798ae82a1a85de to your computer and use it in GitHub Desktop.
Save ismell/9ae5a9798ae82a1a85de to your computer and use it in GitHub Desktop.
Mongo 2.6 positional operator matching incorrect sub document
db.bug.save({
"_id" : ObjectId("53e29d8473636f0664000000"),
"dimensions" : [
{
"_id" : ObjectId("53e29d8473636f0664010000"),
"values" : []
},
{
"_id" : ObjectId("53e29d8473636f06640b0000"),
"values": []
},
{
"_id" : ObjectId("53e29d8473636f06640c0000"),
"values" : [
{
"_id" : ObjectId("53e29d8473636f06640e0000"),
"weight" : 12
},
{
"_id" : ObjectId("53e29d8473636f06640f0000"),
"weight" : 11
},
{
"_id" : ObjectId("53e29d8473636f0664100000"),
"weight" : 3
},
{
"_id" : ObjectId("53e29d8473636f0664110000"),
"weight" : 17
},
{
"_id" : ObjectId("53e29d8473636f0664120000"),
"weight" : 35
}
]
},
]
});
{
"_id" : ObjectId("53e29d8473636f0664000000"),
"dimensions" : [
{
"_id" : ObjectId("53e29d8473636f0664010000"),
"values" : [ ]
},
{
"_id" : ObjectId("53e29d8473636f06640b0000"),
"values" : [ ]
},
{
"_id" : ObjectId("53e29d8473636f06640c0000"),
"values" : [
{
"_id" : ObjectId("53e29d8473636f06640e0000"),
"weight" : 0 // Correct
},
{
"_id" : ObjectId("53e29d8473636f06640f0000"),
"weight" : 11
},
{
"_id" : ObjectId("53e29d8473636f0664100000"),
"weight" : 3
},
{
"_id" : ObjectId("53e29d8473636f0664110000"),
"weight" : 17
},
{
"_id" : ObjectId("53e29d8473636f0664120000"),
"weight" : 35
}
]
}
]
}
{
"_id" : ObjectId("53e29d8473636f0664000000"),
"dimensions" : [
{
"_id" : ObjectId("53e29d8473636f0664010000"),
"values" : [ ]
},
{
"_id" : ObjectId("53e29d8473636f06640b0000"),
"values" : [ ]
},
{
"_id" : ObjectId("53e29d8473636f06640c0000"),
"values" : [
{
"_id" : ObjectId("53e29d8473636f06640e0000"),
"weight" : 12 // this value should have been updated
},
{
"_id" : ObjectId("53e29d8473636f06640f0000"),
"weight" : 11
},
{
"_id" : ObjectId("53e29d8473636f0664100000"),
"weight" : 0 // Incorrect sub document selected
},
{
"_id" : ObjectId("53e29d8473636f0664110000"),
"weight" : 17
},
{
"_id" : ObjectId("53e29d8473636f0664120000"),
"weight" : 35
}
]
}
]
}
db.bug.update({
"_id":ObjectId('53e29d8473636f0664000000'),
"dimensions._id":ObjectId('53e29d8473636f06640c0000'),
"dimensions.2.values._id":ObjectId('53e29d8473636f06640e0000')
},{
"$set":{
"dimensions.2.values.$.weight": 0
}
});
db.bug.find({
"_id":ObjectId('53e29d8473636f0664000000'),
"dimensions._id":ObjectId('53e29d8473636f06640c0000'),
"dimensions.2.values._id":ObjectId('53e29d8473636f06640e0000')
});
@gurix
Copy link

gurix commented Sep 26, 2014

For mongodb 2.6 it looks like this should work...

db.bug.update({
  "_id":ObjectId('53e29d8473636f0664000000'),
  "dimensions._id":ObjectId('53e29d8473636f06640c0000')
},{
  "$set":{
  "dimensions.$.values.1.weight": 0
}
});

Indeed it's a complete different kind of updating :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment