New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mingo

Package Overview
Dependencies
Maintainers
1
Versions
143
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mingo

JavaScript implementation of MongoDB query language

  • 0.8.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
88K
decreased by-31.73%
Maintainers
1
Weekly downloads
 
Created
Source

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. This library is self-contained and has zero-dependencies

version build status

Installing

$ npm install mingo

In browser

<script type="text/javascript" src="./dist/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');
// 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 }
});

Searching and Filtering

// 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);

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);

// shorthand
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); // log filtered outputs
    // ex. { name: 'Dinah Sauve', _id: 49 }
});

file.pipe(JSONStream.parse("*")).pipe(qs);

Backbone Integration

// 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();

Documentation

Contributing

License

MIT

Keywords

FAQs

Package last updated on 08 Dec 2016

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc