Is there a way to use MongoDB query objects to filter regular JavaScript arrays?

In MongoDB, you can use JSON-style objects such as in the following to query a collection:

db.things.find({ x : { $ne : 3 }, y : 'foo' });

I'd like to reuse that { x : { $ne : 3 }, y : 'foo' } bit and use it to filter an array of JavaScript objects.

Is there any code/library out there that can do that, and that supports all the query options (or as much as makes sense anyway)?

-------------Problems Reply------------

Ok, so here's another try:

sift.js (npm: sift) by Craig Condon is a MongoDB-inspired array filtering library. It’s a bit like an alternative to Underscore for people who love MongoDB. Sift.js supports operators like $in and $gt, but can also filter arrays based on functions and even works with deeply-nested objects in arrays.

Craig has provided a few examples that should look familiar to Mongo users:

var sift = require('sift');

sift({ $in: ['hello','world'] }, ['hello','sifted','array!']); //
['hello']

Source: http://dailyjs.com/2012/01/04/node-roundup/

Underscore.js is a great library to do map/reduce kind of jobs on javascript structures. Highly recommended.

I dont think you can just use the mongodb filters in normal js arrays. Because you need to understand the fact that

The filters specified in mongodb are evaluated in mongodb indexes not in the javascript result set

Means the filters evaluated(translated) to query against a index not the js. So what you are asking is a DSL on top of mongodb(or JS) which will evaluate the mongodb index filters in the JS array.

I dont think its needed since both serves the different purposes (Though its possible(difficult) to write custom DSL). Also there are major frameworks like underscore.js already provide a ways to handle these.

You can use https://github.com/mirek/node-json-criteria library, which evaluates critera queries in MongoDB format on JSON objects.

Category:javascript Views:0 Time:2012-01-01

Related post

  • MongoDB : How can i insert a javascript array with PHP (instead of an object)? 2011-05-26

    $update = $col->update(array('_id' => 'test'), array('$push' => array('friends' => 1)), array('safe' => true, 'upsert' => true)); $test = $col->findOne(array('_id' => 'test')); debug(compact('update', 'test')); Does not work a

  • MongoDB query with an 'or' condition 2010-01-05

    So I have an embedded document that tracks group memberships. Each embedded document has an ID pointing to the group in another collection, a start date, and an optional expire date. I want to query for current members of a group. "Current" means the

  • MongoDB query AND/OR Precedent 2010-12-18

    In MongoDB, if I wanted to express A AND (B OR C), I can easily do by: db.demo.find( { a: 1, $or: [ {b:1}, {c:1} ] }); But if I wanted (A AND B) OR C -- it does not seem to work. If I try: db.demo.find( { $or: [ {a:1,b:1}, {c:1} ] }); the above is tr

  • Mongodb Query To select records having a given key 2011-01-03

    The records in my database are {"_id":"1","fn":"sagar","ln":"Varpe"} {"_id":"1","fn":"sag","score":"10"} {"_id":"1","ln":"ln1","score":"10"} {"_id":"1","ln":"ln2"} I need to design a MongoDB query to find all records that have a given key. For exampl

  • MongoDB query based on count of embedded document 2011-01-16

    Suppose I have: Order: {_id: ..., items: [...]} How to filter orders which have item number greater than 5? --------------Solutions------------- You can use the $where operator. > db.orders.save({Order: {items: [1,2]}}) > db.orders.save({Order:

  • Raw mongodb query in rails 2011-04-22

    I have a Rails/Mongoid app that displays data client-side using highcharts. Right now, to pass the data to Javascript, I use a Mongoid query and Rails' as_json/to_json in the template file. This seems very inefficient to me because the json data has

  • Casbah & Rogue for MongoDB - Querying capabilities 2011-05-10

    I'm currently using Casbah with MongoDB for implementing web service. I am having no problems with it so far. I am using Scala as well. However, I was just curious to know if there is something better than Casbah for doing a lot of find/findOne type

  • Doctrine\ODM\MongoDB\Query\Builder serialization breaks mongo connection 2011-06-14

    I have an object that has an instance of Doctrine\ODM\MongoDB\Query\Builder . When I serialize my object, unserialize it, and then try to use the Builder, I get the following error: Fatal error: Uncaught exception 'MongoException' with message 'The M

  • How to convert such sql query to mongodb query 2011-08-19

    I need to execute such sql query on mongo(I mean mongodb query should be logically equal to the sql). So here is the SQL query: SELECT * FROM table1 ALIAS1 WHERE field1=1 AND field2=2 AND ( EXISTS( SELECT * FROM table2 where table2.field33 = ALIAS1.f

  • How to stream MongoDB Query Results with nodejs? 2011-09-10

    I have been searching for an example of how I can stream the result of a MongoDB query to a nodejs client. All solutions I have found so far seem to read the query result at once and then send the result back to the server. Instead, I would (obviousl

  • Problems with MongoDB query 2011-10-12

    I am developing an API using Codeigniter and Alex Bilbies oAuth2 and MongoDB libraries. I need to do a MongoDB query where I do a query for a document with _id of 1 and access token is null. I have this so far but it does not work: $exists_query = $t

  • MongoDB: query against value in nested array? 2011-12-26

    I have documents with such kind of schema: { ... "coverages" : [ [ "GB", "WC1" ], [ "GB", "SE2" ], ... ] } I want to find all documents where 'coverages' contains an array second element of which starts with 'WC', i.e. coverages matches with ['GB', /

  • Slow MongoDB query: can you explain why? 2012-01-04

    I have a MongoDB query that's taking an unreasonably long time to run, but it: is only scanning 6 objects hits an index consistently takes ~1500ms (wasn't paging or otherwise occupied) index miss% is 0 in mongostat It showed up in the profiler (witho

  • MongoDB query comparing 2 fields in same collection without $where 2012-01-12

    Does MongoDB supports comparing two fields in same collection by using native operators (not $where and JavaScript)? I already looked at similar questions and all answers used $where / JavaScript. MongoDB documentation clearly states that: JavaScript

  • Mongodb query from php - can't getting it working 2012-01-27

    Trying to do a simple mongodb query that drives me mad.... I have the following table/db: [_id] => MongoId Object ( [$id] => 4f22efa1ef9dec8495b374bc ) [h1] => a [h2] => b [h3] => c [_id] => MongoId Object ( [$id] => 4f22efa1ef9d

  • MongoDB query filter price 2012-03-09

    I'm faily new to MongoDB, and trying to filter a price within a range. Instead of just filtering on a specific element, I first need to check if a special price exists. If a special price do exist, and it's within the from/to date, it should be used

  • How does Meteor receive updates to the results of a MongoDB query? 2012-04-11

    I asked a question a few months ago, to which Meteor seems to have the answer. Which, if any, of the NoSQL databases can provide stream of *changes* to a query result set? How does Meteor receive updates to the results of a MongoDB query? Thanks, Chr

  • Optimize MongoDB Query or Index 2012-04-11

    I am running a quite ordinary MongoDB query, nothing really complex or special and I am wondering if the time it takes (> 1 sec) is normal or if there's something wrong with my indexes. I provided an index for this specific query and explain() als

  • MongoDB + PHP: How do you query object sizes? 2012-05-02

    MongoDB + PHP: How do you query the size of a document stored in a mongodb object? The document limit, at the time of this writing (recently raised from 4MB), is 16MB. How can I query the size of a Document using the ObjectId? --------------Solutions

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.140 (s). 11 q(s)