Fixed problem

pull/2/head
Louis Chatriot 9 years ago
parent 28de9768bb
commit 72387fdabc
  1. 16
      lib/indexes.js
  2. 9
      test/db.test.js
  3. 14
      test/indexes.test.js

@ -246,13 +246,23 @@ function append (array, toAppend) {
* @return {Array of documents} * @return {Array of documents}
*/ */
Index.prototype.getMatching = function (value) { Index.prototype.getMatching = function (value) {
var res, self = this; var self = this;
if (!util.isArray(value)) { if (!util.isArray(value)) {
return this.tree.search(value); return this.tree.search(value);
} else { } else {
res = []; var _res = {}, res = [];
value.forEach(function (v) { append(res, self.getMatching(v)); });
value.forEach(function (v) {
self.getMatching(v).forEach(function (doc) {
_res[doc._id] = doc;
});
});
Object.keys(_res).forEach(function (_id) {
res.push(_res[_id]);
});
return res; return res;
} }
}; };

@ -2479,6 +2479,15 @@ describe('Database', function () {
}); // ==== End of 'Persisting indexes' ==== }); // ==== End of 'Persisting indexes' ====
it('Results of getMatching should never contain duplicates', function (done) {
d.ensureIndex({ fieldName: 'bad' });
d.insert({ bad: ['a', 'b'] }, function () {
var res = d.getCandidates({ bad: { $in: ['a', 'b'] } });
res.length.should.equal(1);
done();
});
});
}); // ==== End of 'Using indexes' ==== // }); // ==== End of 'Using indexes' ==== //

@ -628,12 +628,16 @@ describe('Indexes', function () {
}); });
it('Can get all documents whose key is in an array of keys', function () { it('Can get all documents whose key is in an array of keys', function () {
// For this test only we have to use objects with _ids as the array version of getMatching
// relies on the _id property being set, otherwise we have to use a quadratic algorithm
// or a fingerprinting algorithm, both solutions too complicated and slow given that live nedb
// indexes documents with _id always set
var idx = new Index({ fieldName: 'tf' }) var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' } , doc1 = { a: 5, tf: 'hello', _id: '1' }
, doc2 = { a: 2, tf: 'bloup' } , doc2 = { a: 2, tf: 'bloup', _id: '2' }
, doc3 = { a: 8, tf: 'world' } , doc3 = { a: 8, tf: 'world', _id: '3' }
, doc4 = { a: 7, tf: 'yes' } , doc4 = { a: 7, tf: 'yes', _id: '4' }
, doc5 = { a: 7, tf: 'yes' } , doc5 = { a: 7, tf: 'yes', _id: '5' }
; ;
idx.insert(doc1); idx.insert(doc1);

Loading…
Cancel
Save