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
Mingo harnesses the power of MongoDB-style queries and allows direct querying of in-memory
javascript objects in both client and server-side environments.
Dependencies
underscore
Installing
$ npm install mingo
In browser
<script type="text/javascript" src="./underscore-min.js"></script>
<script type="text/javascript" src="./mingo.min.js"></script>
Features
- Supports Dot Notation for both '<array>.<index>' and '<document>.<field>' selectors
- Query and Projection Operators
- Array Operators (
$all
, $elemMatch
, $size
) - Comparisons Operators (
$gt
, $gte
, $lt
, $lte
, $ne
, $nin
, $in
) - Element Operators (
$exists
, $type
) - Evaluation Operators (
$regex
, $mod
, $where
) - Logical Operators (
$and
, $or
, $nor
, $not
)
- Aggregation Framework Operators
- Pipeline Operators (
$group
, $match
, $project
, $sort
, $limit
, $unwind
, $skip
) - Group Operators (
$addToSet
, $sum
, $max
, $min
, $avg
, $push
, $first
, $last
) - Projection Operators (
$elemMatch
, $slice
) - Arithmetic Operators (
$add
, $divide
, $mod
, $multiply
, $subtract
) - Array Operators (
$size
) - Boolean Operators (
$and
, $or
, $not
) - Comparisons Operators (
$cmp
, $gt
, $gte
, $lt
, $lte
, $ne
, $nin
, $in
) - Conditional Operators (
$cond
, $ifNull
) - Date Operators (
$dayOfYear
, $dayOfMonth
, $dayOfWeek
, $year
, $month
, $week
, $hour
, $minute
, $second
, $millisecond
, $dateToString
) - Literal Operators (
$literal
) - Set Operators (
$setEquals
, $setIntersection
, $setDifference
, $setUnion
, $setIsSubset
, $anyElementTrue
, $allElementsTrue
) - String Operators (
$strcasecmp
, $concat
, $substr
, $toLower
, $toUpper
) - Variable Operators (
$map
, $let
)
- Support for custom operators
- BackboneJS Integration
- Match against user-defined types
- JSON stream filtering and projection. NodeJS only
For documentation on using query operators see mongodb
Usage
var Mingo = require('mingo');
Mingo.setup({
key: '_id'
});
var query = new Mingo.Query({
type: "homework",
score: { $gte: 50 }
});
Searching and Filtering
var cursor = query.find(collection);
cursor.sort({student_id: 1, score: -1})
.skip(100)
.limit(100);
cursor.count();
while (cursor.hasNext()) {
console.log(cursor.next());
}
cursor.first();
cursor.last();
cursor.all();
var result = query.remove(collection);
Aggregation Pipeline
var agg = new Mingo.Aggregator([
{'$match': { "type": "homework"}},
{'$group':{'_id':'$student_id', 'score':{$min:'$score'}}},
{'$sort':{'_id': 1, 'score': 1}}
]);
var result = agg.run(collection);
result = Mingo.aggregate(
collection,
[
{'$match': { "type": "homework"}},
{'$group':{'_id':'$student_id', 'score':{$min:'$score'}}},
{'$sort':{'_id': 1, 'score': 1}}
]
);
Stream Filtering
var JSONStream = require('JSONStream'),
fs = require('fs'),
Mingo = require('mingo');
var query = new Mingo.Query({
scores: { $elemMatch: {type: "exam", score: {$gt: 90}} }
}, {name: 1});
file = fs.createReadStream('./students.json');
var qs = query.stream();
qs.on('data', function (data) {
console.log(data);
});
file.pipe(JSONStream.parse("*")).pipe(qs);
Backbone Integration
var Grades = Backbone.Collection.extend(Mingo.CollectionMixin);
var 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.first();
Documentation
TODO
- Geospatial Query Operators (
$geoWithin
, $geoIntersects
, $near
, $nearSphere
) - Geometry Specifiers (
$geometry
, $maxDistance
, $center
, $centerSphere
, $box
, $polygon
)
License
MIT