louischatriot / nedb

The JavaScript Database, for Node.js, nw.js, electron and the browser
MIT License
13.5k stars 1.03k forks source link

Support for $all operator for array fields #278

Open TomV opened 9 years ago

TomV commented 9 years ago

Given the dataset:

{"_id": 1, "name": "Joe", "tags": ['sailing', 'chess', 'whisky', 'cheese', 'football']}
{"_id": 2, "name": "Jane", "tags": ['wine', 'cheese', 'ballet', 'opera']}
{"_id": 3, "name": "Mary", "tags": ['sailing', 'wine', 'cheese', 'hiking']}
{"_id": 4, "name": "Mike", "tags": ['hiking', 'chess', 'beer', 'wine', 'skiing', 'football']}

To find all the documents/people who have an interest in ALL of these 'sailing', 'wine', 'cheese', I can do this in mongo:

var query = db.find(
{  "tags": {
     $all: ['sailing', 'wine', 'cheese']
    } 
});

But nedb does not support {$all: [array]}, so the alternative is to use $and, and repeat the field to check:

var query = db.find( { 
  $and: 
      [
        {"tags": 'sailing'},
        {"tags": 'wine'},
        {"tags": 'cheese'}
      ]
});

It would be more concise if the mogodb "$all" operator were supported. Louis - can you nudge me in the right direction to make write a patch to support this?

popod commented 8 years ago

I need the same feature.. Is their any solution ?

@TomV what about your solution performance ?

JulianLang commented 3 years ago

Hi, any news on this one? This feature would be really nice, especially as MongoDb has it as well :)