From f48a1565bc46cb75f359c522eb724a62ec016e42 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sun, 20 Oct 2013 12:41:47 +0200 Subject: [PATCH] Handles removes in the good cases --- lib/indexes.js | 8 +++++++- test/indexes.test.js | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/indexes.js b/lib/indexes.js index 89d988c..cb000b3 100644 --- a/lib/indexes.js +++ b/lib/indexes.js @@ -125,7 +125,13 @@ Index.prototype.remove = function (doc) { if (key === undefined && this.sparse) { return; } - this.tree.delete(key, doc); + if (!util.isArray(key)) { + this.tree.delete(key, doc); + } else { + _.uniq(key, projectForUnique).forEach(function (_key) { + self.tree.delete(_key, doc); + }); + } }; diff --git a/test/indexes.test.js b/test/indexes.test.js index c2482ba..13399d0 100644 --- a/test/indexes.test.js +++ b/test/indexes.test.js @@ -177,6 +177,25 @@ describe('Indexes', function () { (function () { idx.insert(obj2); }).should.throw(); }); + it('When removing a document, remove it from the index at all array elements', function () { + var obj = { tf: ['aa', 'aa'], really: 'yeah' } + , obj2 = { tf: ['cc', 'aa', 'cc'], yes: 'indeed' } + , idx = new Index({ fieldName: 'tf' }) + ; + + idx.insert(obj); + idx.insert(obj2); + idx.getMatching('aa').length.should.equal(2); + idx.getMatching('aa').indexOf(obj).should.not.equal(-1); + idx.getMatching('aa').indexOf(obj2).should.not.equal(-1); + idx.getMatching('cc').length.should.equal(1); + + idx.remove(obj2); + idx.getMatching('aa').length.should.equal(1); + idx.getMatching('aa').indexOf(obj).should.not.equal(-1); + idx.getMatching('aa').indexOf(obj2).should.equal(-1); + idx.getMatching('cc').length.should.equal(0); + }); }); // ==== End of 'Array fields' ==== //