18

Is there a javascript library that will allow me to express object predicates in a DSL similar to MongoDB's query language? For the sake of clarity in a large program, I'd like to be able to say:

var obj = { 
    a: 1, 
    b: 'abcdefg' 
}, qry = { 
    a: { $gt: 0 }, 
    b: /^abc/ 
}; 

if(query(qry).matches(obj)) { 
    // do something appropriate since 
} 

instead of:

var obj = { 
    a: 1, 
    b: 'abcdefg' 
}; 
if(obj.a>0 && qry.b.test(obj.b)) { 
    // do something appropriate 
} 

I'm using Node.js, so anything on NPM would be great. It would be an added bonus if the library can select objects out of an array as well as just matching individual objects.

I reviewed these two related questions, but they weren't particularly helpful for my situation:

Karol Selak
  • 4,248
  • 6
  • 35
  • 65
RubyTuesdayDONO
  • 2,350
  • 2
  • 25
  • 37
  • Possible duplicate of [Is there a way to use MongoDB query objects to filter regular JavaScript arrays?](http://stackoverflow.com/questions/8695718/is-there-a-way-to-use-mongodb-query-objects-to-filter-regular-javascript-arrays) – Karol Selak Feb 08 '17 at 11:06

5 Answers5

20

OK I found the answer: Sift.js

Now for the long answer: This has been asked and answered before. The salient points are:

  • Use Sift if you really want Mongo syntax
  • If you want to be more mainstream, use Underscore.js like everyone else. It has heaps of handy functions in addition to the fact that it basically does what sift does with a slightly different syntax.
  • You may not need any library at all - modern browsers support many useful functions directly on the Array prototype, like filter() for example.

As a final note, mongodb-riff appears to be trying to do something similar but currently the page states clearly that it doesn't work - perhaps it's abandoned. But his readme is at least of value :-), he mentions sift and Query Engine which looks more mature, though too complicated for me!

Personally I'm going to go with Underscore because now that I've looked into it for the first time, I realise that it has heaps of handy stuff I need, plus I really only wanted to do simple functions like what would be _.find() in Underscore. But I guess if you want to do more complicated mongo-like queries, you'll do it in less LOC with Sift.

Fletch
  • 4,829
  • 2
  • 41
  • 55
  • well, i figured there'd be something - and why not on Github? i probably should have looked for myself, but i was hoping someone else had come across it already. let's see if you can steal the prize from @Vivin ^_^! – RubyTuesdayDONO Mar 20 '13 at 18:15
  • 1
    awesome! [Sift.js](https://github.com/crcn/sift.js) looks just right. i do like and use [Underscore](http://underscorejs.org/)/[Lodash](http://lodash.com/), but need to re-use predicates already stored inside Mongo. thank you! – RubyTuesdayDONO Mar 25 '13 at 10:38
  • 1
    oh, and thank you for finding [the original question](http://stackoverflow.com/questions/8695718]. i searched but didn't find it ^_^; – RubyTuesdayDONO Mar 25 '13 at 10:48
  • There's also balupton's [query-engine](https://github.com/bevry) which is feature rich and mature. – alexandru.topliceanu Apr 24 '13 at 04:39
9

Check out Mingo

I implemented it after finding no suitable alternative.

It is still actively being developed but is usable. Test coverage is not complete.

Usable from both browser and nodejs

[Edit]

This library is now the most complete implementation of MongoDB's query language for the frontend.

kofrasa
  • 2,090
  • 2
  • 14
  • 10
  • Both sift and Mingo are great, but [MSON](https://github.com/redgeoff/mson) is switching to Mingo as Mingo has support for aggregations. – redgeoff Jan 31 '21 at 15:53
2

https://github.com/mirek/node-json-criteria library does exactly that - evaluates criteria queries on JSON objects using MongoDB query format.

Mirek Rusin
  • 18,820
  • 3
  • 43
  • 36
1

lodash.js

You want to use lodash.js. Its a drop in replacement for underscore.js. The performance is twice as fast.

http://lodash.com/

Donny V.
  • 22,248
  • 13
  • 65
  • 79
0

The closest I could find was linq.js, which is LINQ for JavaScript. Hopefully this will be of some help to you.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
  • 1
    i don't think it's what i'm looking for - but that's definitely not your fault! maybe no one has made anything like that yet. i'll consider [linq.js](http://linqjs.codeplex.com/) and see if it helps at all. thank you, @Vivin! – RubyTuesdayDONO Mar 14 '13 at 18:58