2

I try to find entries by indices:

var questions = db.addCollection('questions',{indices: ['key']});
questions.insert({
key: 1,
quest: 'any idea'
},
{
  key: 2,
  quest: 'no idea'
});

questions.find({'key':1});

but I get no result. Dumping the database shows the entries. Any idea ?

vp_arth
  • 14,461
  • 4
  • 37
  • 66

1 Answers1

1

author of LokiJS here. Just for the sake of accuracy the problem does not lie in the string vs number, but in the syntax used for the query, which should not be find({x: 1, y: 1}) but find({x:1}, {y: 1}). Queries with multiple properties will maybe be supported in the future but the recommended way is to use findAnd and findOr, or better yet, use DynamicViews when possible.

update: the correct syntax to do this is now: find({ '$and': [{x:1}, {y: 1}]}).

Joe Minichino
  • 2,793
  • 20
  • 20
  • 1
    Hi Joe, nice to get an official comment. My feeling is, it would be more common to be able to access by full key with the normal find and no need of findAnd. I think this was the source of trying this that way. I agree, in the case of no constructed primary key the common findAnd would be the common way. Nice work what you have done with LokiJS - RESPECT !! – Danny Apr 26 '15 at 07:33
  • 1
    @Danny cheers! Passing different query objects like `{ x: 1 }, { y: 1}` does an implicit `and` operation, In any case, 1.4 will introduce `ExactIndex` which will group records with a particular value, which will resolve many of these scenarios. The `find` method is more to appease Mongo lovers (I am one of those too :D) but not idiomatic LokiJS. – Joe Minichino Apr 26 '15 at 14:05