Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
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.
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 } ]
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 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 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.
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. This library is self-contained and has zero-dependencies
$ npm install mingo
In browser
<script type="text/javascript" src="./dist/mingo.min.js"></script>
$all
, $elemMatch
, $size
)$gt
, $gte
, $lt
, $lte
, $ne
, $nin
, $in
)$exists
, $type
)$regex
, $mod
, $where
)$and
, $or
, $nor
, $not
)$group
,$match
,$project
,$sort
,$limit
,$unwind
,$skip
,$count
,$replaceRoot
,$redact
,$sample
,$sortByCount
,$addFields
)$addToSet
, $sum
, $max
, $min
, $avg
, $push
, $first
, $last
)$elemMatch
, $slice
)$add
, $divide
, $mod
, $multiply
, $subtract
)$size
)$and
, $or
, $not
)$cmp
, $gt
, $gte
, $lt
, $lte
, $ne
, $nin
, $in
)$cond
, $switch
, $ifNull
)$dayOfYear
, $dayOfMonth
, $dayOfWeek
, $year
, $month
, $week
, $hour
, $minute
, $second
, $millisecond
, $dateToString
)$literal
)$setEquals
, $setIntersection
, $setDifference
, $setUnion
, $setIsSubset
, $anyElementTrue
, $allElementsTrue
)$strcasecmp
, $concat
, $substr
, $toLower
, $toUpper
)$map
, $let
)$$ROOT
,$$CURRENT
,$$DESCEND
,$$PRUNE
,$$KEEP
)For documentation on using query operators see mongodb
var Mingo = require('mingo');
// or just access *Mingo* global in browser
// setup the key field for your collection
Mingo.setup({
key: '_id' // default
});
// create a query with criteria
// find all grades for homework with score >= 50
var query = new Mingo.Query({
type: "homework",
score: { $gte: 50 }
});
// filter collection with find()
var 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 (
var result = query.remove(collection);
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);
// shorthand
result = Mingo.aggregate(
collection,
[
{'$match': { "type": "homework"}},
{'$group':{'_id':'$student_id', 'score':{$min:'$score'}}},
{'$sort':{'_id': 1, 'score': 1}}
]
);
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); // log filtered outputs
// ex. { name: 'Dinah Sauve', _id: 49 }
});
file.pipe(JSONStream.parse("*")).pipe(qs);
// using with Backbone
var Grades = Backbone.Collection.extend(Mingo.CollectionMixin);
var 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);
// print grade with the lowest score
cursor.first();
MIT
v0.9.0 / 2017-02-06
$$ROOT
,$$CURRENT
)$redact
,$addFields
,$sample
,$sortByCount
,$count
,$replaceRoot
)$switch
conditional operator$ifNull
conditional operator$in
and $nin
as aggregation comparison operatorsFAQs
MongoDB query language for in-memory objects
The npm package mingo receives a total of 59,989 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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.