From f08d912c5cd1cbf832d026a8e6084299583d687a Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 23 May 2013 19:14:59 +0200 Subject: [PATCH] Implemented operator --- lib/model.js | 6 ++++++ test/model.test.js | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/model.js b/lib/model.js index 1d6346c..d8edec1 100644 --- a/lib/model.js +++ b/lib/model.js @@ -340,6 +340,12 @@ comparisonFunctions.$in = function (a, b) { return false; }; +comparisonFunctions.$nin = function (a, b) { + if (!util.isArray(b)) { throw "$nin operator called with a non-array"; } + + return !comparisonFunctions.$in(a, b); +}; + /** * Match any of the subqueries diff --git a/test/model.test.js b/test/model.test.js index 720a572..6be09b4 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -495,6 +495,19 @@ describe('Model', function () { (function () { model.match({ a: 5 }, { a: { $in: 5 } }); }).should.throw(); }); + it('$nin', function () { + model.match({ a: 5 }, { a: { $nin: [6, 8, 9] } }).should.equal(true); + model.match({ a: 6 }, { a: { $nin: [6, 8, 9] } }).should.equal(false); + model.match({ a: 7 }, { a: { $nin: [6, 8, 9] } }).should.equal(true); + model.match({ a: 8 }, { a: { $nin: [6, 8, 9] } }).should.equal(false); + model.match({ a: 9 }, { a: { $nin: [6, 8, 9] } }).should.equal(false); + + // Matches if field doesn't exist + model.match({ a: 9 }, { b: { $nin: [6, 8, 9] } }).should.equal(true); + + (function () { model.match({ a: 5 }, { a: { $in: 5 } }); }).should.throw(); + }); + });