Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Javascript library can evaluate MongoDB-like query predicates against an object?

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:

  • Implementing goMongoDB-like Query expression object evaluation
  • Evaluating MongoDB-like JSON Queries in PHP
like image 309
RubyTuesdayDONO Avatar asked Mar 13 '13 22:03

RubyTuesdayDONO


People also ask

Is MongoDB related to JavaScript?

MongoDB create databaseThe mongo tool is an interactive JavaScript shell interface to MongoDB, which provides an interface for systems administrators as well as a way for developers to test queries and operations directly with the database. We create a testdb database and insert eight documents in the cars collection.

How do you retrieve data in MongoDB using JavaScript?

To select data from a table in MongoDB, we can also use the find() method. The find() method returns all occurrences in the selection. The first parameter of the find() method is a query object. In this example we use an empty query object, which selects all documents in the collection.

What is MongoDB in JavaScript?

MongoDB is a document-based NoSQL database that stores data in BSON (JSON-like) format. JavaScript is unanimously the most popular language for building web applications. It can also be used for building server-side applications usingNode. js. Node.


2 Answers

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.

like image 139
Fletch Avatar answered Oct 21 '22 07:10

Fletch


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.

like image 40
kofrasa Avatar answered Oct 21 '22 06:10

kofrasa