Info
Several operations ($set and $inc tested, there may be others) can create a nameless property which becomes difficult to remove and causes updates to partially and silently fail.
Top User Comments
scotthernandez commented on Tue, 11 Feb 2014 12:55:29 +0000:
This was fixed in the update refactoring for 2.6; this is the output from the latest dev release, 2.5.5+:
> db.testGuy.insert({Name: "Test1"})
...
> db.testGuy.update({Name: "Test1"}, {$inc: {"TestObject..Value": 1}})
SingleWriteResult({
"writeErrors" : [
{
"index" : 0,
"code" : 16840,
"errmsg" : "The update path 'TestObject..Value' contains an empty field name, which is not allowed.",...
Steps to Reproduce
To get a nameless property from shell:
db.testGuy.insert({Name: "Test1"})
db.testGuy.update({Name: "Test1"}, {$inc: {"TestObject..Value": 1}})
db.testGuy.findOne({Name: "Test1"})
{
"Name" : "Test1",
"TestObject" : {
"" : {
"Value" : 1
}
},
"_id" : ObjectId("52f93e1be856377627267033")
}
Yikes! Now what happens if I try to mess around with TestObject?
db.testGuy.update({Name: "Test1"}, {$inc: "TestObject.TestProperty.Value": 1}})
db.testGuy.findOne({Name: "Test1"})
{
"Name" : "Test1",
"TestObject" : {
"" : {
"Value" : 1
}
},
"_id" : ObjectId("52f93e1be856377627267033")
}
db.testGuy.update({Name: "Test1"}, {$inc: {VeryCoolValue: 1, "TestObject.AnotherValue": 1}})
{
"Name" : "Test1",
"TestObject" : {
"" : {
"Value" : 1
}
},
"VeryCoolValue" : 1,
"_id" : ObjectId("52f93e1be856377627267033")
}
That last one shows the danger of this kind of situation: the updates partially complete and report success even though half of my update actually failed.