PageRenderTime 54ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/mingo/README.md

https://bitbucket.org/nodeforever/estrutura_basica_api
Markdown | 151 lines | 121 code | 30 blank | 0 comment | 0 complexity | 335d99696faa8b76a5051a277780013b MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, 0BSD, JSON, Unlicense, BSD-2-Clause
  1. # Mingo
  2. JavaScript implementation of MongoDB query language
  3. Mingo harnesses the power of MongoDB-style queries and allows direct querying of in-memory
  4. javascript objects in both client and server-side environments. This library is self-contained and has zero-dependencies
  5. [![version](https://img.shields.io/npm/v/mingo.svg)](https://www.npmjs.org/package/mingo)
  6. [![build status](https://secure.travis-ci.org/kofrasa/mingo.png)](http://travis-ci.org/kofrasa/mingo)
  7. ## Installing
  8. ```$ npm install mingo```
  9. In browser
  10. ```html
  11. <script type="text/javascript" src="./dist/mingo.min.js"></script>
  12. ```
  13. ## Features
  14. - Supports Dot Notation for both '_&lt;array&gt;.&lt;index&gt;_' and '_&lt;document&gt;.&lt;field&gt;_' selectors
  15. - Query and Projection Operators
  16. - [Array Operators](https://docs.mongodb.com/manual/reference/operator/query-array/)
  17. - [Comparisons Operators](https://docs.mongodb.com/manual/reference/operator/query-comparison/)
  18. - [Element Operators](https://docs.mongodb.com/manual/reference/operator/query-element/)
  19. - [Evaluation Operators](https://docs.mongodb.com/manual/reference/operator/query-evaluation/)
  20. - [Logical Operators](https://docs.mongodb.com/manual/reference/operator/query-logical/)
  21. - Aggregation Framework Operators
  22. - [Pipeline Operators](https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/)
  23. - [Group Operators](https://docs.mongodb.com/manual/reference/operator/aggregation-group/)
  24. - [Projection Operators](https://docs.mongodb.com/manual/reference/operator/projection/)
  25. - [Arithmetic Operators](https://docs.mongodb.com/manual/reference/operator/aggregation-arithmetic/)
  26. - [Array Operators](https://docs.mongodb.com/manual/reference/operator/aggregation-array/)
  27. - [Boolean Operators](https://docs.mongodb.com/manual/reference/operator/aggregation-boolean/)
  28. - [Comparisons Operators](https://docs.mongodb.com/manual/reference/operator/aggregation-comparison/)
  29. - [Conditional Operators](https://docs.mongodb.com/manual/reference/operator/aggregation-conditional/)
  30. - [Date Operators](https://docs.mongodb.com/manual/reference/operator/aggregation-date/)
  31. - [Literal Operators](https://docs.mongodb.com/manual/reference/operator/aggregation-literal/)
  32. - [Set Operators](https://docs.mongodb.com/manual/reference/operator/aggregation-set/)
  33. - [String Operators](https://docs.mongodb.com/manual/reference/operator/aggregation-string/)
  34. - [Variable Operators](https://docs.mongodb.com/manual/reference/operator/aggregation-projection/)
  35. - Support for adding custom operators
  36. - Match against user-defined types
  37. - Support for aggregaion variables
  38. - [`$$ROOT`,`$$CURRENT`,`$$DESCEND`,`$$PRUNE`,`$$KEEP`](https://docs.mongodb.com/manual/reference/aggregation-variables/)
  39. - Support integrating with custom collections via mixin
  40. - Query filter and projection streaming. See [mingo-stream](https://github.com/kofrasa/mingo-stream)
  41. For documentation on using query operators see [mongodb](http://docs.mongodb.org/manual/reference/operator/query/)
  42. ## Usage
  43. ```js
  44. var Mingo = require('mingo');
  45. // or just access *Mingo* global in browser
  46. // setup the key field for your collection
  47. Mingo.setup({
  48. key: '_id' // default
  49. });
  50. // create a query with criteria
  51. // find all grades for homework with score >= 50
  52. var query = new Mingo.Query({
  53. type: "homework",
  54. score: { $gte: 50 }
  55. });
  56. ```
  57. ## Searching and Filtering
  58. ```js
  59. // `collection` is an Array of objects you want to query
  60. // filter collection with find()
  61. var cursor = query.find(collection);
  62. // shorthand with query criteria
  63. // cursor = Mingo.find(collection, criteria);
  64. // sort, skip and limit by chaining
  65. cursor.sort({student_id: 1, score: -1})
  66. .skip(100)
  67. .limit(100);
  68. // count matches
  69. cursor.count();
  70. // iterate cursor
  71. // iteration is forward only
  72. while (cursor.hasNext()) {
  73. console.log(cursor.next());
  74. }
  75. // use first(), last() and all() to retrieve matched objects
  76. cursor.first();
  77. cursor.last();
  78. cursor.all();
  79. // Filter non-matched objects (
  80. var result = query.remove(collection);
  81. ```
  82. ## Aggregation Pipeline
  83. ```js
  84. var agg = new Mingo.Aggregator([
  85. {'$match': { "type": "homework"}},
  86. {'$group':{'_id':'$student_id', 'score':{$min:'$score'}}},
  87. {'$sort':{'_id': 1, 'score': 1}}
  88. ]);
  89. var result = agg.run(collection);
  90. // shorthand
  91. result = Mingo.aggregate(
  92. collection,
  93. [
  94. {'$match': { "type": "homework"}},
  95. {'$group':{'_id':'$student_id', 'score':{$min:'$score'}}},
  96. {'$sort':{'_id': 1, 'score': 1}}
  97. ]
  98. );
  99. ```
  100. ## Integration with custom collection
  101. ```js
  102. // using Backbone.Collection as an example (any user-defined object will do)
  103. var Grades = Backbone.Collection.extend(Mingo.CollectionMixin);
  104. // `collection` is an array of objects
  105. var grades = new Grades(collection);
  106. // find students with grades less than 50 in homework or quiz
  107. // sort by score ascending and type descending
  108. cursor = grades.query({
  109. $or: [{type: "quiz", score: {$lt: 50}}, {type: "homework", score: {$lt: 50}}]
  110. }).sort({score: 1, type: -1}).limit(10);
  111. // return grade with the lowest score
  112. cursor.first();
  113. ```
  114. The collection to mixin needs to provide a method with signature `toJSON() -> Array[Object]`.
  115. ## Documentation
  116. - [API](https://github.com/kofrasa/mingo/wiki/API)
  117. - [Custom Operators](https://github.com/kofrasa/mingo/wiki/Custom-Operators)
  118. ## Contributing
  119. - Submit pull requests to the [development](https://github.com/kofrasa/mingo/tree/development) branch
  120. ## License
  121. MIT