Throw error in Mongo.Collection when selector is array, fixes #4804

This commit is contained in:
Sashko Stubailo
2015-07-28 20:11:13 -07:00
parent 0c93aeb6b6
commit 02dfd2c8f0
2 changed files with 31 additions and 0 deletions

View File

@@ -328,6 +328,12 @@ Mongo.Collection._rewriteSelector = function (selector) {
if (LocalCollection._selectorIsId(selector))
selector = {_id: selector};
if (_.isArray(selector)) {
// This is consistent with the Mongo console itself; if we don't do this
// check passing an empty array ends up selecting all items
throw new Error("Mongo selector can't be an array.");
}
if (!selector || (('_id' in selector) && !selector._id))
// can't match anything
return {_id: Random.id()};

View File

@@ -3120,3 +3120,28 @@ Meteor.isServer && Tinytest.add("mongo-livedata - npm modules", function (test)
test.isTrue(rawDb);
test.isTrue(rawDb.admin);
});
if (Meteor.isServer) {
Tinytest.add("mongo-livedata - update/remove don't accept an array as a selector #4804", function (test) {
var collection = new Mongo.Collection(Random.id());
_.times(10, function () {
collection.insert({ data: "Hello" });
});
test.equal(collection.find().count(), 10);
// Test several array-related selectors
_.each([[], [1, 2, 3], [{}]], function (selector) {
test.throws(function () {
collection.remove(selector);
});
test.throws(function () {
collection.update(selector, {$set: 5});
});
});
test.equal(collection.find().count(), 10);
});
}