Can return all matching documents from the index

pull/2/head
Louis Chatriot 12 years ago
parent 1d246b6a94
commit 4c9113712f
  1. 8
      lib/indexes.js
  2. 23
      test/indexes.test.js

@ -75,8 +75,14 @@ Index.prototype.update = function (oldDoc, newDoc) {
/** /**
* Search * Get all documents in index that match the query on fieldName
* For now only works with field equality (i.e. can't use the index for $lt query for example)
* @param {Thing} value Value to match the key against
* @return {Array od documents}
*/ */
Index.prototype.getMatching = function (value) {
return this.tree.search(value);
};
// Interface // Interface

@ -192,4 +192,27 @@ describe('Indexing', function () {
}); // ==== End of 'Update' ==== // }); // ==== End of 'Update' ==== //
describe('Get matching documents', function () {
it('Get all documents where fieldName is equal to the given value, or an empty array if no match', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
, doc4 = { a: 23, tf: 'world' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.insert(doc4);
assert.deepEqual(idx.getMatching('bloup'), [doc3]);
assert.deepEqual(idx.getMatching('world'), [doc2, doc4]);
assert.deepEqual(idx.getMatching('nope'), []);
});
}); // ==== End of 'Get matching documents' ==== //
}); });

Loading…
Cancel
Save