aggregation_cursor.js 10 KB

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