![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
JavaScript implementation of MongoDB query language
$ 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
mingo.setup({
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 }
});
query.test(someObject)
// `collection` is an Array of objects you want to query
// 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})
.skip(100)
.limit(100);
// count matches
cursor.count();
// iterate cursor
// iteration is forward only
while (cursor.hasNext()) {
console.log(cursor.next());
}
// use first(), last() and all() to retrieve matched objects
cursor.first();
cursor.last();
cursor.all();
// Filter non-matched objects (
console.log(query.remove(collection));
let agg = new mingo.Aggregator([
{'$match': { "type": "homework"}},
{'$group':{'_id':'$student_id', 'score':{$min:'$score'}}},
{'$sort':{'_id': 1, 'score': 1}}
]);
let result = agg.run(collection);
// shorthand
result = mingo.aggregate(
collection,
[
{'$match': { "type": "homework"}},
{'$group':{'_id':'$student_id', 'score':{$min:'$score'}}},
{'$sort':{'_id': 1, 'score': 1}}
]
);
// 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
cursor.first();
The collection to mixin needs to provide a method with signature toJSON() -> Array[Object]
.
make
to ensure build and tests passMIT
FAQs
MongoDB query language for in-memory objects
The npm package mingo receives a total of 134,130 weekly downloads. As such, mingo popularity was classified as popular.
We found that mingo demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.