JavaScript implementation of MongoDB query language
Latest commit a1d7de6 Sep 9, 2018


JavaScript implementation of MongoDB query language

version build status npm Codecov Code Quality: Javascript Total Alerts


$ npm install mingo


For documentation on using query operators see mongodb



On the server side

// Use as es6 module
import mingo from 'mingo'

// or vanilla nodeJS
var mingo = require('mingo')

For the browser

// minified UMD module
<script type="text/javascript" src="./dist/mingo.min.js"></script>

// or gzipped UMD module
<script type="text/javascript" src="./dist/mingo.min.js.gz"></script>

Tiny configuration if needed

// setup the key field for your collection
    key: '_id' // default

Using query object to test objects

// create a query with criteria
// find all grades for homework with score >= 50
let query = new mingo.Query({
    type: "homework",
    score: { $gte: 50 }

// test if an object matches query

Searching and Filtering

// input is either an Array or any iterable source (i.e Object{next:Function}) including ES6 generators.

// filter collection with find()
let cursor = query.find(collection)

// shorthand with query criteria
cursor = mingo.find(collection, criteria)

// sort, skip and limit by chaining
cursor.sort({student_id: 1, score: -1})

// count matches. exhausts cursor

// classic cursor iterator (old school)
while (cursor.hasNext()) {

// ES6 iterators (new cool)
for (let value of cursor) {

// all() to retrieve matched objects. exhausts cursor

Aggregation Pipeline

let agg = new mingo.Aggregator([
    {'$match': { "type": "homework"}},
    {'$group':{'_id':'$student_id', 'score':{$min:'$score'}}},
    {'$sort':{'_id': 1, 'score': 1}}

// return an iterator for streaming results
let stream =

// return all results. same as `stream.all()`
let result =

Integration with custom collection

// using Backbone.Collection as an example (any user-defined object will do)
let Grades = Backbone.Collection.extend(mingo.CollectionMixin)

// `collection` is an array of objects
let grades = new Grades(collection)

// find students with grades less than 50 in homework or quiz
// sort by score ascending and type descending
cursor = grades.query({
  $or: [{type: "quiz", score: {$lt: 50}}, {type: "homework", score: {$lt: 50}}]
}).sort({score: 1, type: -1}).limit(10)

// return grade with the lowest score

The collection to mixin needs to provide a method with signature toJSON() -> Array[Object].


  • Alternative to writing lots of custom code for transforming collection of objects
  • Quick validation of MongoDB queries without the need for a database
  • MongoDB query language is among the best in the market and is well documented


  • Submit pull requests to the development branch
  • Squash changes into one commit
  • Run make to ensure build and tests pass