Skip to content

Commit

Permalink
Allow $exists operator to work on arrays
Browse files Browse the repository at this point in the history
Fix #581
  • Loading branch information
JamesMGreene committed Mar 11, 2019
1 parent aa3302e commit 3397b58
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ comparisonFunctions.$exists = function (value, exists) {
}

if (value === undefined) {
return !exists
return !exists;
} else {
return exists;
}
Expand All @@ -661,6 +661,7 @@ comparisonFunctions.$elemMatch = function (obj, value) {
};
arrayComparisonFunctions.$size = true;
arrayComparisonFunctions.$elemMatch = true;
arrayComparisonFunctions.$exists = true;


/**
Expand Down
14 changes: 14 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,20 @@ describe('Model', function () {
model.match({ childrens: [ { name: "Huey", age: 3 }, { name: "Dewey", age: 7 }, { name: "Louie", age: 12 } ] }, { "childrens": { $elemMatch: { name: "Dewey", age: { $gt: 6, $lt: 7 } } } }).should.equal(false);
model.match({ childrens: [ { name: "Huey", age: 3 }, { name: "Dewey", age: 7 }, { name: "Louie", age: 12 } ] }, { "childrens": { $elemMatch: { name: "Louie", age: { $gt: 6, $lte: 7 } } } }).should.equal(false);
});

it('$exists', function () {
model.match({ a: [5, 6] }, { a: { $exists: true } }).should.equal(true);

model.match({ a: [5] }, { a: { $exists: true } }).should.equal(true);
model.match({ a: [5] }, { b: { $exists: false } }).should.equal(true);

model.match({ a: [0] }, { a: { $exists: true } }).should.equal(true);
model.match({ a: [null] }, { a: { $exists: true } }).should.equal(true);
model.match({ a: [undefined] }, { a: { $exists: true } }).should.equal(true);

model.match({ a: [] }, { a: { $exists: false } }).should.equal(false);
model.match({ a: [] }, { a: { $exists: true } }).should.equal(true);
});
});


Expand Down

0 comments on commit 3397b58

Please sign in to comment.