aggregation_cursor.js 11 KB

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