aggregation_cursor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. "use strict";
  2. var inherits = require('util').inherits
  3. , MongoError = require('mongodb-core').MongoError
  4. , Readable = require('stream').Readable || require('readable-stream').Readable
  5. , Define = require('./metadata')
  6. , CoreCursor = require('./cursor');
  7. /**
  8. * @fileOverview The **AggregationCursor** class is an internal class that embodies an aggregation cursor on MongoDB
  9. * allowing for iteration over the results returned from the underlying query. It supports
  10. * one by one document iteration, conversion to an array or can be iterated as a Node 0.10.X
  11. * or higher stream
  12. *
  13. * **AGGREGATIONCURSOR Cannot directly be instantiated**
  14. * @example
  15. * var MongoClient = require('mongodb').MongoClient,
  16. * test = require('assert');
  17. * // Connection url
  18. * var url = 'mongodb://localhost:27017/test';
  19. * // Connect using MongoClient
  20. * MongoClient.connect(url, function(err, db) {
  21. * // Create a collection we want to drop later
  22. * var col = db.collection('createIndexExample1');
  23. * // Insert a bunch of documents
  24. * col.insert([{a:1, b:1}
  25. * , {a:2, b:2}, {a:3, b:3}
  26. * , {a:4, b:4}], {w:1}, function(err, result) {
  27. * test.equal(null, err);
  28. * // Show that duplicate records got dropped
  29. * col.aggregation({}, {cursor: {}}).toArray(function(err, items) {
  30. * test.equal(null, err);
  31. * test.equal(4, items.length);
  32. * db.close();
  33. * });
  34. * });
  35. * });
  36. */
  37. /**
  38. * Namespace provided by the browser.
  39. * @external Readable
  40. */
  41. /**
  42. * Creates a new Aggregation Cursor instance (INTERNAL TYPE, do not instantiate directly)
  43. * @class AggregationCursor
  44. * @extends external:Readable
  45. * @fires AggregationCursor#data
  46. * @fires AggregationCursor#end
  47. * @fires AggregationCursor#close
  48. * @fires AggregationCursor#readable
  49. * @return {AggregationCursor} an AggregationCursor instance.
  50. */
  51. var AggregationCursor = function(bson, ns, cmd, options, topology, topologyOptions) {
  52. CoreCursor.apply(this, Array.prototype.slice.call(arguments, 0));
  53. var state = AggregationCursor.INIT;
  54. var streamOptions = {};
  55. // MaxTimeMS
  56. var maxTimeMS = null;
  57. // Get the promiseLibrary
  58. var promiseLibrary = options.promiseLibrary;
  59. // No promise library selected fall back
  60. if(!promiseLibrary) {
  61. promiseLibrary = typeof global.Promise == 'function' ?
  62. global.Promise : require('es6-promise').Promise;
  63. }
  64. // Set up
  65. Readable.call(this, {objectMode: true});
  66. // Internal state
  67. this.s = {
  68. // MaxTimeMS
  69. maxTimeMS: maxTimeMS
  70. // State
  71. , state: state
  72. // Stream options
  73. , streamOptions: streamOptions
  74. // BSON
  75. , bson: bson
  76. // Namespae
  77. , ns: ns
  78. // Command
  79. , cmd: cmd
  80. // Options
  81. , options: options
  82. // Topology
  83. , topology: topology
  84. // Topology Options
  85. , topologyOptions: topologyOptions
  86. // Promise library
  87. , promiseLibrary: promiseLibrary
  88. }
  89. }
  90. /**
  91. * AggregationCursor stream data event, fired for each document in the cursor.
  92. *
  93. * @event AggregationCursor#data
  94. * @type {object}
  95. */
  96. /**
  97. * AggregationCursor stream end event
  98. *
  99. * @event AggregationCursor#end
  100. * @type {null}
  101. */
  102. /**
  103. * AggregationCursor stream close event
  104. *
  105. * @event AggregationCursor#close
  106. * @type {null}
  107. */
  108. /**
  109. * AggregationCursor stream readable event
  110. *
  111. * @event AggregationCursor#readable
  112. * @type {null}
  113. */
  114. // Inherit from Readable
  115. inherits(AggregationCursor, Readable);
  116. // Extend the Cursor
  117. for(var name in CoreCursor.prototype) {
  118. AggregationCursor.prototype[name] = CoreCursor.prototype[name];
  119. }
  120. var define = AggregationCursor.define = new Define('AggregationCursor', AggregationCursor, true);
  121. /**
  122. * Set the batch size for the cursor.
  123. * @method
  124. * @param {number} value The batchSize for the cursor.
  125. * @throws {MongoError}
  126. * @return {AggregationCursor}
  127. */
  128. AggregationCursor.prototype.batchSize = function(value) {
  129. if(this.s.state == AggregationCursor.CLOSED || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true });
  130. if(typeof value != 'number') throw MongoError.create({message: "batchSize requires an integer", drvier:true });
  131. if(this.s.cmd.cursor) this.s.cmd.cursor.batchSize = value;
  132. this.setCursorBatchSize(value);
  133. return this;
  134. }
  135. define.classMethod('batchSize', {callback: false, promise:false, returns: [AggregationCursor]});
  136. /**
  137. * Add a geoNear stage to the aggregation pipeline
  138. * @method
  139. * @param {object} document The geoNear stage document.
  140. * @return {AggregationCursor}
  141. */
  142. AggregationCursor.prototype.geoNear = function(document) {
  143. this.s.cmd.pipeline.push({$geoNear: document});
  144. return this;
  145. }
  146. define.classMethod('geoNear', {callback: false, promise:false, returns: [AggregationCursor]});
  147. /**
  148. * Add a group stage to the aggregation pipeline
  149. * @method
  150. * @param {object} document The group stage document.
  151. * @return {AggregationCursor}
  152. */
  153. AggregationCursor.prototype.group = function(document) {
  154. this.s.cmd.pipeline.push({$group: document});
  155. return this;
  156. }
  157. define.classMethod('group', {callback: false, promise:false, returns: [AggregationCursor]});
  158. /**
  159. * Add a limit stage to the aggregation pipeline
  160. * @method
  161. * @param {number} value The state limit value.
  162. * @return {AggregationCursor}
  163. */
  164. AggregationCursor.prototype.limit = function(value) {
  165. this.s.cmd.pipeline.push({$limit: value});
  166. return this;
  167. }
  168. define.classMethod('limit', {callback: false, promise:false, returns: [AggregationCursor]});
  169. /**
  170. * Add a match stage to the aggregation pipeline
  171. * @method
  172. * @param {object} document The match stage document.
  173. * @return {AggregationCursor}
  174. */
  175. AggregationCursor.prototype.match = function(document) {
  176. this.s.cmd.pipeline.push({$match: document});
  177. return this;
  178. }
  179. define.classMethod('match', {callback: false, promise:false, returns: [AggregationCursor]});
  180. /**
  181. * Add a maxTimeMS stage to the aggregation pipeline
  182. * @method
  183. * @param {number} value The state maxTimeMS value.
  184. * @return {AggregationCursor}
  185. */
  186. AggregationCursor.prototype.maxTimeMS = function(value) {
  187. if(this.s.topology.lastIsMaster().minWireVersion > 2) {
  188. this.s.cmd.maxTimeMS = value;
  189. }
  190. return this;
  191. }
  192. define.classMethod('maxTimeMS', {callback: false, promise:false, returns: [AggregationCursor]});
  193. /**
  194. * Add a out stage to the aggregation pipeline
  195. * @method
  196. * @param {number} destination The destination name.
  197. * @return {AggregationCursor}
  198. */
  199. AggregationCursor.prototype.out = function(destination) {
  200. this.s.cmd.pipeline.push({$out: destination});
  201. return this;
  202. }
  203. define.classMethod('out', {callback: false, promise:false, returns: [AggregationCursor]});
  204. /**
  205. * Add a project stage to the aggregation pipeline
  206. * @method
  207. * @param {object} document The project stage document.
  208. * @return {AggregationCursor}
  209. */
  210. AggregationCursor.prototype.project = function(document) {
  211. this.s.cmd.pipeline.push({$project: document});
  212. return this;
  213. }
  214. define.classMethod('project', {callback: false, promise:false, returns: [AggregationCursor]});
  215. /**
  216. * Add a lookup stage to the aggregation pipeline
  217. * @method
  218. * @param {object} document The lookup stage document.
  219. * @return {AggregationCursor}
  220. */
  221. AggregationCursor.prototype.lookup = function(document) {
  222. this.s.cmd.pipeline.push({$lookup: document});
  223. return this;
  224. }
  225. define.classMethod('lookup', {callback: false, promise:false, returns: [AggregationCursor]});
  226. /**
  227. * Add a redact stage to the aggregation pipeline
  228. * @method
  229. * @param {object} document The redact stage document.
  230. * @return {AggregationCursor}
  231. */
  232. AggregationCursor.prototype.redact = function(document) {
  233. this.s.cmd.pipeline.push({$redact: document});
  234. return this;
  235. }
  236. define.classMethod('redact', {callback: false, promise:false, returns: [AggregationCursor]});
  237. /**
  238. * Add a skip stage to the aggregation pipeline
  239. * @method
  240. * @param {number} value The state skip value.
  241. * @return {AggregationCursor}
  242. */
  243. AggregationCursor.prototype.skip = function(value) {
  244. this.s.cmd.pipeline.push({$skip: value});
  245. return this;
  246. }
  247. define.classMethod('skip', {callback: false, promise:false, returns: [AggregationCursor]});
  248. /**
  249. * Add a sort stage to the aggregation pipeline
  250. * @method
  251. * @param {object} document The sort stage document.
  252. * @return {AggregationCursor}
  253. */
  254. AggregationCursor.prototype.sort = function(document) {
  255. this.s.cmd.pipeline.push({$sort: document});
  256. return this;
  257. }
  258. define.classMethod('sort', {callback: false, promise:false, returns: [AggregationCursor]});
  259. /**
  260. * Add a unwind stage to the aggregation pipeline
  261. * @method
  262. * @param {number} field The unwind field name.
  263. * @return {AggregationCursor}
  264. */
  265. AggregationCursor.prototype.unwind = function(field) {
  266. this.s.cmd.pipeline.push({$unwind: field});
  267. return this;
  268. }
  269. define.classMethod('unwind', {callback: false, promise:false, returns: [AggregationCursor]});
  270. AggregationCursor.prototype.get = AggregationCursor.prototype.toArray;
  271. // Inherited methods
  272. define.classMethod('toArray', {callback: true, promise:true});
  273. define.classMethod('each', {callback: true, promise:false});
  274. define.classMethod('forEach', {callback: true, promise:false});
  275. define.classMethod('next', {callback: true, promise:true});
  276. define.classMethod('close', {callback: true, promise:true});
  277. define.classMethod('isClosed', {callback: false, promise:false, returns: [Boolean]});
  278. define.classMethod('rewind', {callback: false, promise:false});
  279. define.classMethod('bufferedCount', {callback: false, promise:false, returns: [Number]});
  280. define.classMethod('readBufferedDocuments', {callback: false, promise:false, returns: [Array]});
  281. /**
  282. * Get the next available document from the cursor, returns null if no more documents are available.
  283. * @function AggregationCursor.prototype.next
  284. * @param {AggregationCursor~resultCallback} [callback] The result callback.
  285. * @throws {MongoError}
  286. * @return {Promise} returns Promise if no callback passed
  287. */
  288. /**
  289. * The callback format for results
  290. * @callback AggregationCursor~toArrayResultCallback
  291. * @param {MongoError} error An error instance representing the error during the execution.
  292. * @param {object[]} documents All the documents the satisfy the cursor.
  293. */
  294. /**
  295. * Returns an array of documents. The caller is responsible for making sure that there
  296. * is enough memory to store the results. Note that the array only contain partial
  297. * results when this cursor had been previouly accessed. In that case,
  298. * cursor.rewind() can be used to reset the cursor.
  299. * @method AggregationCursor.prototype.toArray
  300. * @param {AggregationCursor~toArrayResultCallback} [callback] The result callback.
  301. * @throws {MongoError}
  302. * @return {Promise} returns Promise if no callback passed
  303. */
  304. /**
  305. * The callback format for results
  306. * @callback AggregationCursor~resultCallback
  307. * @param {MongoError} error An error instance representing the error during the execution.
  308. * @param {(object|null)} result The result object if the command was executed successfully.
  309. */
  310. /**
  311. * Iterates over all the documents for this cursor. As with **{cursor.toArray}**,
  312. * not all of the elements will be iterated if this cursor had been previouly accessed.
  313. * In that case, **{cursor.rewind}** can be used to reset the cursor. However, unlike
  314. * **{cursor.toArray}**, the cursor will only hold a maximum of batch size elements
  315. * at any given time if batch size is specified. Otherwise, the caller is responsible
  316. * for making sure that the entire result can fit the memory.
  317. * @method AggregationCursor.prototype.each
  318. * @param {AggregationCursor~resultCallback} callback The result callback.
  319. * @throws {MongoError}
  320. * @return {null}
  321. */
  322. /**
  323. * Close the cursor, sending a AggregationCursor command and emitting close.
  324. * @method AggregationCursor.prototype.close
  325. * @param {AggregationCursor~resultCallback} [callback] The result callback.
  326. * @return {Promise} returns Promise if no callback passed
  327. */
  328. /**
  329. * Is the cursor closed
  330. * @method AggregationCursor.prototype.isClosed
  331. * @return {boolean}
  332. */
  333. /**
  334. * Execute the explain for the cursor
  335. * @method AggregationCursor.prototype.explain
  336. * @param {AggregationCursor~resultCallback} [callback] The result callback.
  337. * @return {Promise} returns Promise if no callback passed
  338. */
  339. /**
  340. * Clone the cursor
  341. * @function AggregationCursor.prototype.clone
  342. * @return {AggregationCursor}
  343. */
  344. /**
  345. * Resets the cursor
  346. * @function AggregationCursor.prototype.rewind
  347. * @return {AggregationCursor}
  348. */
  349. /**
  350. * The callback format for the forEach iterator method
  351. * @callback AggregationCursor~iteratorCallback
  352. * @param {Object} doc An emitted document for the iterator
  353. */
  354. /**
  355. * The callback error format for the forEach iterator method
  356. * @callback AggregationCursor~endCallback
  357. * @param {MongoError} error An error instance representing the error during the execution.
  358. */
  359. /*
  360. * Iterates over all the documents for this cursor using the iterator, callback pattern.
  361. * @method AggregationCursor.prototype.forEach
  362. * @param {AggregationCursor~iteratorCallback} iterator The iteration callback.
  363. * @param {AggregationCursor~endCallback} callback The end callback.
  364. * @throws {MongoError}
  365. * @return {null}
  366. */
  367. AggregationCursor.INIT = 0;
  368. AggregationCursor.OPEN = 1;
  369. AggregationCursor.CLOSED = 2;
  370. module.exports = AggregationCursor;