Implemented comparator

pull/2/head
Louis Chatriot 12 years ago
parent 48084e2ff5
commit 2f2b9ab58f
  1. 14
      lib/model.js
  2. 20
      test/model.test.js

@ -503,6 +503,20 @@ comparisonFunctions.$nin = function (a, b) {
return !comparisonFunctions.$in(a, b); return !comparisonFunctions.$in(a, b);
}; };
comparisonFunctions.$exists = function (value, exists) {
if (exists || exists === '') { // This will be true for all values of exists except false, null, undefined and 0
exists = true; // That's strange behaviour (we should only use true/false) but that's the way Mongo does it...
} else {
exists = false;
}
if (value === undefined) {
return !exists
} else {
return exists;
}
};
/** /**
* Match any of the subqueries * Match any of the subqueries

@ -770,7 +770,7 @@ describe('Model', function () {
// General behaviour is tested in the block about $lt. Here we just test operators work // General behaviour is tested in the block about $lt. Here we just test operators work
describe('Other comparison operators: $lte, $gt, $gte, $ne, $in', function () { describe('Other comparison operators: $lte, $gt, $gte, $ne, $in, $exists', function () {
it('$lte', function () { it('$lte', function () {
model.match({ a: 5 }, { a: { $lte: 6 } }).should.equal(true); model.match({ a: 5 }, { a: { $lte: 6 } }).should.equal(true);
@ -819,6 +819,24 @@ describe('Model', function () {
(function () { model.match({ a: 5 }, { a: { $in: 5 } }); }).should.throw(); (function () { model.match({ a: 5 }, { a: { $in: 5 } }); }).should.throw();
}); });
it('$exists', function () {
model.match({ a: 5 }, { a: { $exists: 1 } }).should.equal(true);
model.match({ a: 5 }, { a: { $exists: true } }).should.equal(true);
model.match({ a: 5 }, { a: { $exists: new Date() } }).should.equal(true);
model.match({ a: 5 }, { a: { $exists: '' } }).should.equal(true);
model.match({ a: 5 }, { a: { $exists: [] } }).should.equal(true);
model.match({ a: 5 }, { a: { $exists: {} } }).should.equal(true);
model.match({ a: 5 }, { a: { $exists: 0 } }).should.equal(false);
model.match({ a: 5 }, { a: { $exists: false } }).should.equal(false);
model.match({ a: 5 }, { a: { $exists: null } }).should.equal(false);
model.match({ a: 5 }, { a: { $exists: undefined } }).should.equal(false);
model.match({ a: 5 }, { b: { $exists: true } }).should.equal(false);
model.match({ a: 5 }, { b: { $exists: false } }).should.equal(true);
});
}); });

Loading…
Cancel
Save