mingo
MongoDB query language for in-memory objects
Install
$ npm install mingo
Features
- Supports Dot Notation for both
<array>.<index>
and<document>.<field>
selectors - Query and Projection Operators
- Aggregation Framework Operators
- Support for adding custom operators using
mingo.addOperators
- Match against user-defined types
- Support for aggregaion variables
- ES6 module compatible
- Support integrating with custom collections via mixin
- Query filtering and aggregation streaming.
For documentation on using query operators see mongodb
Documentation
Usage
// Use as es6 module // or vanilla nodeJSconst mingo =
Changes in 3.0.0
Default exports and operators
The default export of the main module only includes Aggregator
, Query
, aggregate()
, find()
, and remove()
.
Only Query and Projection operators are loaded by default when using the main module.
This is done using the side-effect module mingo/init/basic
, and also automatically includes pipeline operators $project
, $skip
, $limit
, and $sort
.
If your application uses most of the available operators or you do not care about bundle size, you can load all operators as shown below.
// Note that doing this effectively imports the entire library into your bundle and unused operators cannot be tree shaked
Or from the node CLI
node -r 'mingo/init/system' myscript.js
Custom Operators
The addOperators
function for registering custom operators and helper constants have been moved to mingo/core
.
The constants OP_XXX
have been deprecated and replace with an enum type OperatorType
also in mingo/core
.
The values defined include;
ACCUMULATOR
EXPRESSION
PIPELINE
PROJECTION
QUERY
Lastly, the function argument to addOperators(operatorType, fn)
now accepts an object with the these two internal functions;
computeValue(obj: object | any[], expr: any, operator: string, options?: ComputeOptions): any
resolve(obj: object | any[], selector: string, options?: ResolveOptions): any
Any extra utility may be imported directly from the specific module.
Importing submodules
Submodule imports are supported for both ES6 and ES5.
The following two examples are equivalent.
ES6
This work natively in typescript since it knows how to load commonJS modules as ES6. You may optionally install the esm module to use this syntax.
ES5
Unlike the ES6 version, it is necessary to specify the operator module in the path to avoid loading any extras
const $unwind = $unwind
Configuration
To support tree-shaking, you may import and register specific operators that will be used in your application.
Using query object to test objects
// create a query with criteria// find all grades for homework with score >= 50let query = type: "homework" score: $gte: 50 ; // test if an object matches queryquery
Searching and Filtering
// input is either an Array or any iterable source (i.e Object{next:Function}) including ES6 generators.let criteria = score: $gt: 10 let query = criteria // filter collection with find()let cursor = query // alternatively use shorthand// cursor = mingo.find(collection, criteria) // sort, skip and limit by chainingcursor // count matches. exhausts cursorcursor // classic cursor iterator (old school)while cursor console // ES6 iterators (new cool)for let value of cursor console // all() to retrieve matched objects. exhausts cursorcursorall
Aggregation Pipeline
// ensure the required operators are preloaded prior to using them. let agg = '$match': "type": "homework" '$group': '_id':'$student_id' 'score':$min:'$score' '$sort': '_id': 1 'score': 1 // return an iterator for streaming resultslet stream = agg // return all results. same as `stream.all()`let result = agg
Benefits
- Better alternative to writing 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
Contributing
- Squash changes into one commit
- Run
npm test
to build and execute unit tests - Submit pull request
License
MIT