JavaScript實現MongoDB查詢語言:Mingo

jopen 9年前發布 | 26K 次閱讀 Mingo MongoDB NoSQL數據庫

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.baiduhome.net/lib/view/home/1422276537734



 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!