LokiJS: a simple search query returns an invalid result

In LokiJS, I try a very simple query (which I assume as AND):

var dbRes = recsCol.find({'format':format, 'cardId':-1}); 

after pasting some data with

 recsCol.insert({format:format, cardId:id, recCardId:key, amount:item[key]}); 

which does not contain cardId of -1.

The query still yields results. Is this expected behavior? If so, how can I make the fields exactly so that I don't get the result in this case?

+4
source share
2 answers

You can do AND in LokiJS, no problem:

 var dbRes = recsCol.find({'$and': [{'format':format}, {'cardId':-1}]}); 

I recommend using search for one-time queries. If the query is run multiple times on result sets that may change, then definitely use a view.

+6
source

By default, all fields in the request object are treated as OR. To do this AND, you will need to change the query syntax to the following:

 recsCol.find({ $and: [ {'format':format}, {'cardId':-1} ] }); 
+4
source

All Articles