What is mingo?
Mingo is a JavaScript library that provides MongoDB-like query and aggregation capabilities for in-memory data processing. It allows you to filter, sort, group, and transform data collections using a syntax similar to MongoDB queries.
What are mingo's main functionalities?
Querying
This feature allows you to perform complex queries on your data collections. The example demonstrates how to filter a list of people to find those older than 26.
const mingo = require('mingo');
const data = [
{ name: 'John', age: 25 },
{ name: 'Jane', age: 30 },
{ name: 'Jim', age: 27 }
];
const query = new mingo.Query({ age: { $gt: 26 } });
const result = query.find(data).all();
console.log(result); // Output: [ { name: 'Jane', age: 30 }, { name: 'Jim', age: 27 } ]
Aggregation
This feature allows you to perform aggregation operations on your data collections. The example demonstrates how to calculate the average salary of people older than 26.
const mingo = require('mingo');
const data = [
{ name: 'John', age: 25, salary: 50000 },
{ name: 'Jane', age: 30, salary: 60000 },
{ name: 'Jim', age: 27, salary: 55000 }
];
const pipeline = [
{ $match: { age: { $gt: 26 } } },
{ $group: { _id: null, averageSalary: { $avg: '$salary' } } }
];
const result = mingo.aggregate(data, pipeline);
console.log(result); // Output: [ { _id: null, averageSalary: 57500 } ]
Projection
This feature allows you to project specific fields from your data collections. The example demonstrates how to retrieve only the 'name' and 'age' fields from a list of people.
const mingo = require('mingo');
const data = [
{ name: 'John', age: 25, salary: 50000 },
{ name: 'Jane', age: 30, salary: 60000 },
{ name: 'Jim', age: 27, salary: 55000 }
];
const query = new mingo.Query({});
const result = query.find(data).project({ name: 1, age: 1 }).all();
console.log(result); // Output: [ { name: 'John', age: 25 }, { name: 'Jane', age: 30 }, { name: 'Jim', age: 27 } ]
Other packages similar to mingo
lodash
Lodash is a JavaScript utility library that provides a wide range of functions for manipulating arrays, objects, and other data structures. While it does not offer MongoDB-like query syntax, it provides similar functionalities for filtering, sorting, and transforming data collections.
underscore
Underscore is another JavaScript utility library similar to Lodash. It provides a set of utility functions for working with arrays, objects, and other data types. Like Lodash, it does not offer MongoDB-like query syntax but provides similar data manipulation capabilities.
json-query
json-query is a library for querying JSON data structures using a simple query language. It provides functionalities for filtering and transforming JSON data, similar to Mingo, but with a different query syntax.
mingo
JavaScript implementation of MongoDB query language
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
- Match against user-defined types
- Support for aggregaion variables
- ES6 module compatible
- Support integrating with custom collections via mixin
- Query filter and projection streaming.
For documentation on using query operators see mongodb
Documentation
Usage
On the server side
import mingo from 'mingo'
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
mingo.setup({
key: '_id'
});
Using query object to test objects
let query = new mingo.Query({
type: "homework",
score: { $gte: 50 }
});
query.test(someObject)
Searching and Filtering
let cursor = query.find(collection)
cursor = mingo.find(collection, criteria)
cursor.sort({student_id: 1, score: -1})
.skip(100)
.limit(100)
cursor.count()
while (cursor.hasNext()) {
console.log(cursor.next())
}
for (let value of cursor) {
console.log(value)
}
cursor.all()
Aggregation Pipeline
let agg = new mingo.Aggregator([
{'$match': { "type": "homework"}},
{'$group':{'_id':'$student_id', 'score':{$min:'$score'}}},
{'$sort':{'_id': 1, 'score': 1}}
])
let stream = agg.stream(collection)
let result = agg.run(collection)
Integration with custom collection
let Grades = Backbone.Collection.extend(mingo.CollectionMixin)
let grades = new Grades(collection)
cursor = grades.query({
$or: [{type: "quiz", score: {$lt: 50}}, {type: "homework", score: {$lt: 50}}]
}).sort({score: 1, type: -1}).limit(10)
cursor.next()
The collection to mixin needs to provide a method with signature toJSON() -> Array[Object]
.
Why
- 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
Contributing
- Squash changes into one commit
- Run
make
to ensure tests pass - Submit pull request
License
MIT