Disallow mixed modifier/non-modifier fields in updates

We expect modifiers to be either modifiers or replacements, not a
mix. MongoDB does this check also, but do it in meteor too as a safety
belt.
This commit is contained in:
Emily Stark
2014-12-16 13:24:39 -08:00
committed by David Glasser
parent d9feb32148
commit d700610397
2 changed files with 15 additions and 5 deletions

View File

@@ -514,10 +514,20 @@ MongoConnection.prototype._update = function (collection_name, selector, mod,
};
var isModificationMod = function (mod) {
for (var k in mod)
if (k.substr(0, 1) === '$')
return true;
return false;
var isReplace = false;
var isModify = false;
for (var k in mod) {
if (k.substr(0, 1) === '$') {
isModify = true;
} else {
isReplace = true;
}
}
if (isModify && isReplace) {
throw new Error(
"Update parameter cannot have both modifier and non-modifier fields.");
}
return isModify;
};
var NUM_OPTIMISTIC_TRIES = 3;

View File

@@ -3068,7 +3068,7 @@ Meteor.isServer && testAsyncMulti("mongo-livedata - update with replace forbidde
test.throws(function () {
c.update(id, { foo3: "bar3", $set: { blah: 1 } });
}, "cannot be mixed");
}, "cannot have both modifier and non-modifier fields");
test.equal(c.findOne(id), { _id: id, foo2: "bar2" });
}
]);