Fixed bug where $and/$or/$nor accepted empty arrays

mongodb doesn't accept it, so we follow its example. Also fixed test
for that.
This commit is contained in:
Christian Schramm
2012-11-16 17:25:32 +01:00
committed by David Glasser
parent 1291546954
commit eca9d705ae
2 changed files with 25 additions and 6 deletions

View File

@@ -580,8 +580,12 @@ Tinytest.add("minimongo - selector_compiler", function (test) {
nomatch({"a.b": {$in: [1, 2, 3]}}, {a: {b: [4]}});
// $or
match({$or: []}, {}); // should throw error!
match({$or: []}, {a: 1}); // should throw error!
test.throws(function () {
match({$or: []}, {});
});
test.throws(function () {
match({$or: []}, {a: 1});
});
match({$or: [{a: 1}]}, {a: 1});
nomatch({$or: [{b: 2}]}, {a: 1});
match({$or: [{a: 1}, {b: 2}]}, {a: 1});
@@ -657,8 +661,12 @@ Tinytest.add("minimongo - selector_compiler", function (test) {
// this is possibly an open-ended task, so we stop here ...
// $nor
match({$nor: []}, {}); // should throw error!
match({$nor: []}, {a: 1}); // should throw error!
test.throws(function () {
match({$nor: []}, {});
});
test.throws(function () {
match({$nor: []}, {a: 1});
});
nomatch({$nor: [{a: 1}]}, {a: 1});
match({$nor: [{b: 2}]}, {a: 1});
nomatch({$nor: [{a: 1}, {b: 2}]}, {a: 1});
@@ -730,8 +738,13 @@ Tinytest.add("minimongo - selector_compiler", function (test) {
nomatch({$nor: [{a: {$not: {$mod: [10, 1]}}}, {a: {$mod: [10, 2]}}]}, {a: 3});
// $and
match({$and: []}, {}); // should throw error!
match({$and: []}, {a: 1}); // should throw error!
test.throws(function () {
match({$and: []}, {});
});
test.throws(function () {
match({$and: []}, {a: 1});
});
match({$and: [{a: 1}]}, {a: 1});
nomatch({$and: [{a: 1}, {a: 2}]}, {a: 1});
nomatch({$and: [{a: 1}, {b: 1}]}, {a: 1});

View File

@@ -321,6 +321,8 @@ LocalCollection._exprForSelector = function (selector, literals) {
LocalCollection._exprForDocumentPredicate = function (op, value, literals) {
if (op === '$or') {
var clauses = [];
if (value.length === 0)
throw Error("$and/$or/$nor must be a nonempty array");
_.each(value, function (c) {
clauses.push(LocalCollection._exprForSelector(c, literals));
});
@@ -330,6 +332,8 @@ LocalCollection._exprForDocumentPredicate = function (op, value, literals) {
if (op === '$and') {
var clauses = [];
if (value.length === 0)
throw Error("$and/$or/$nor must be a nonempty array");
_.each(value, function (c) {
clauses.push(LocalCollection._exprForSelector(c, literals));
});
@@ -339,6 +343,8 @@ LocalCollection._exprForDocumentPredicate = function (op, value, literals) {
if (op === '$nor') {
var clauses = [];
if (value.length === 0)
throw Error("$and/$or/$nor must be a nonempty array");
_.each(value, function (c) {
clauses.push("!(" + LocalCollection._exprForSelector(c, literals) + ")");
});