MingoMongoDB 查询的 JavaScript 实现
Mingo 是 MongoDB 查询语言的 JavaScript 实现。
Mingo 利用 MongoDB 风格查询,在客户端或者服务器端环境下,允许直接查询内存的 JavaScript 对象。
特性:
-
Comparisons Operators ($gt, $gte, $lt, $lte, $ne, $nin, $in)
-
Logical Operators ($and, $or, $nor, $not)
-
Evaluation Operators ($regex, $mod, $where)
-
Array Operators ($all, $elemMatch, $size)
-
Element Operators ($exists, $type)
-
Aggregation Pipeline Operators ($group, $match, $project, $sort, $limit, $unwind, $skip)
-
Conditional Operators ($cond, $ifNull)
-
Group Operators ($addToSet, $sum, $max, $min, $avg, $push, $first, $last)
-
Arithmetic Operators ($add, $divide, $mod, $multiply, $subtract)
-
String Operators ($cmp, $strcasecmp, $concat, $substr, $toLower, $toUpper)
-
Set Operators ($setEquals, $setIntersection, $setDifference, $setUnion, $setIsSubset, $anyElementTrue, $allElementsTrue)
-
Projection Operators ($elemMatch, $slice)
-
JSON stream filtering and projection. NodeJS only
使用
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);