diff --git a/lib/model.js b/lib/model.js index b719e7f..4a5af8f 100644 --- a/lib/model.js +++ b/lib/model.js @@ -303,7 +303,9 @@ function areComparable (a, b) { /** - * Arithmetic comparators + * Arithmetic and comparison operators + * @param {Native value} a Value in the object + * @param {Native value} b Value in the query */ comparisonFunctions.$lt = function (a, b) { return areComparable(a, b) && a < b; @@ -321,6 +323,11 @@ comparisonFunctions.$gte = function (a, b) { return areComparable(a, b) && a >= b; }; +comparisonFunctions.$ne = function (a, b) { + if (!a) { return true; } + return !areThingsEqual(a, b); +}; + /** * Match any of the subqueries diff --git a/test/model.test.js b/test/model.test.js index 037f319..4259d25 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -459,7 +459,7 @@ describe('Model', function () { // General behaviour is tested in the block about $lt. Here we just test operators work - describe('Other comparison operators: $lte, $gt, $gte', function () { + describe('Other comparison operators: $lte, $gt, $gte, $ne', function () { it('$lte', function () { model.match({ a: 5 }, { a: { $lte: 6 } }).should.equal(true); @@ -479,6 +479,12 @@ describe('Model', function () { model.match({ a: 5 }, { a: { $gte: 4 } }).should.equal(true); }); + it('$ne', function () { + model.match({ a: 5 }, { a: { $ne: 4 } }).should.equal(true); + model.match({ a: 5 }, { a: { $ne: 5 } }).should.equal(false); + model.match({ a: 5 }, { b: { $ne: 5 } }).should.equal(true); + }); + });