JavaScript实现MongoDB查询语言:Mingo

jopen 10年前

Mingo是MongoDB查询语言的JavaScript实现。

安装

$ npm install mingo

在浏览器中

<!-- DO NOT FORGET Underscore -->   <script type="text/javascript" src="./underscore-min.js"></script>  <script type="text/javascript" src="./mingo-min.js"></script>

特性

  • 比较操作符($gt, $gte, $lt, $lte, $ne, $nin, $in)
  • 逻辑操作符 Operators ($and, $or, $nor, $not)
  • Evaluation Operators ($regex, $mod, $where)
  • 数组操作符 ($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);

项目主页:http://www.open-open.com/lib/view/home/1422276537734