cursor.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. 'use strict';
  2. const Transform = require('stream').Transform;
  3. const PassThrough = require('stream').PassThrough;
  4. const deprecate = require('util').deprecate;
  5. const handleCallback = require('./utils').handleCallback;
  6. const ReadPreference = require('./core').ReadPreference;
  7. const MongoError = require('./core').MongoError;
  8. const CoreCursor = require('./core/cursor').CoreCursor;
  9. const CursorState = require('./core/cursor').CursorState;
  10. const Map = require('./core').BSON.Map;
  11. const maybePromise = require('./utils').maybePromise;
  12. const executeOperation = require('./operations/execute_operation');
  13. const formattedOrderClause = require('./utils').formattedOrderClause;
  14. const each = require('./operations/cursor_ops').each;
  15. const CountOperation = require('./operations/count');
  16. /**
  17. * @fileOverview The **Cursor** class is an internal class that embodies a 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 4.X
  20. * or higher stream
  21. *
  22. * **CURSORS Cannot directly be instantiated**
  23. * @example
  24. * const MongoClient = require('mongodb').MongoClient;
  25. * const test = require('assert');
  26. * // Connection url
  27. * const url = 'mongodb://localhost:27017';
  28. * // Database Name
  29. * const dbName = 'test';
  30. * // Connect using MongoClient
  31. * MongoClient.connect(url, function(err, client) {
  32. * // Create a collection we want to drop later
  33. * const col = client.db(dbName).collection('createIndexExample1');
  34. * // Insert a bunch of documents
  35. * col.insert([{a:1, b:1}
  36. * , {a:2, b:2}, {a:3, b:3}
  37. * , {a:4, b:4}], {w:1}, function(err, result) {
  38. * test.equal(null, err);
  39. * // Show that duplicate records got dropped
  40. * col.find({}).toArray(function(err, items) {
  41. * test.equal(null, err);
  42. * test.equal(4, items.length);
  43. * client.close();
  44. * });
  45. * });
  46. * });
  47. */
  48. /**
  49. * Namespace provided by the code module
  50. * @external CoreCursor
  51. * @external Readable
  52. */
  53. // Flags allowed for cursor
  54. const flags = ['tailable', 'oplogReplay', 'noCursorTimeout', 'awaitData', 'exhaust', 'partial'];
  55. const fields = ['numberOfRetries', 'tailableRetryInterval'];
  56. /**
  57. * Creates a new Cursor instance (INTERNAL TYPE, do not instantiate directly)
  58. * @class Cursor
  59. * @extends external:CoreCursor
  60. * @extends external:Readable
  61. * @property {string} sortValue Cursor query sort setting.
  62. * @property {boolean} timeout Is Cursor able to time out.
  63. * @property {ReadPreference} readPreference Get cursor ReadPreference.
  64. * @fires Cursor#data
  65. * @fires Cursor#end
  66. * @fires Cursor#close
  67. * @fires Cursor#readable
  68. * @return {Cursor} a Cursor instance.
  69. * @example
  70. * Cursor cursor options.
  71. *
  72. * collection.find({}).project({a:1}) // Create a projection of field a
  73. * collection.find({}).skip(1).limit(10) // Skip 1 and limit 10
  74. * collection.find({}).batchSize(5) // Set batchSize on cursor to 5
  75. * collection.find({}).filter({a:1}) // Set query on the cursor
  76. * collection.find({}).comment('add a comment') // Add a comment to the query, allowing to correlate queries
  77. * collection.find({}).addCursorFlag('tailable', true) // Set cursor as tailable
  78. * collection.find({}).addCursorFlag('oplogReplay', true) // Set cursor as oplogReplay
  79. * collection.find({}).addCursorFlag('noCursorTimeout', true) // Set cursor as noCursorTimeout
  80. * collection.find({}).addCursorFlag('awaitData', true) // Set cursor as awaitData
  81. * collection.find({}).addCursorFlag('partial', true) // Set cursor as partial
  82. * collection.find({}).addQueryModifier('$orderby', {a:1}) // Set $orderby {a:1}
  83. * collection.find({}).max(10) // Set the cursor max
  84. * collection.find({}).maxTimeMS(1000) // Set the cursor maxTimeMS
  85. * collection.find({}).min(100) // Set the cursor min
  86. * collection.find({}).returnKey(true) // Set the cursor returnKey
  87. * collection.find({}).setReadPreference(ReadPreference.PRIMARY) // Set the cursor readPreference
  88. * collection.find({}).showRecordId(true) // Set the cursor showRecordId
  89. * collection.find({}).sort([['a', 1]]) // Sets the sort order of the cursor query
  90. * collection.find({}).hint('a_1') // Set the cursor hint
  91. *
  92. * All options are chainable, so one can do the following.
  93. *
  94. * collection.find({}).maxTimeMS(1000).maxScan(100).skip(1).toArray(..)
  95. */
  96. class Cursor extends CoreCursor {
  97. constructor(topology, ns, cmd, options) {
  98. super(topology, ns, cmd, options);
  99. if (this.operation) {
  100. options = this.operation.options;
  101. }
  102. // Tailable cursor options
  103. const numberOfRetries = options.numberOfRetries || 5;
  104. const tailableRetryInterval = options.tailableRetryInterval || 500;
  105. const currentNumberOfRetries = numberOfRetries;
  106. // Get the promiseLibrary
  107. const promiseLibrary = options.promiseLibrary || Promise;
  108. // Internal cursor state
  109. this.s = {
  110. // Tailable cursor options
  111. numberOfRetries: numberOfRetries,
  112. tailableRetryInterval: tailableRetryInterval,
  113. currentNumberOfRetries: currentNumberOfRetries,
  114. // State
  115. state: CursorState.INIT,
  116. // Promise library
  117. promiseLibrary,
  118. // explicitlyIgnoreSession
  119. explicitlyIgnoreSession: !!options.explicitlyIgnoreSession
  120. };
  121. // Optional ClientSession
  122. if (!options.explicitlyIgnoreSession && options.session) {
  123. this.cursorState.session = options.session;
  124. }
  125. // Translate correctly
  126. if (this.options.noCursorTimeout === true) {
  127. this.addCursorFlag('noCursorTimeout', true);
  128. }
  129. // Get the batchSize
  130. let batchSize = 1000;
  131. if (this.cmd.cursor && this.cmd.cursor.batchSize) {
  132. batchSize = this.cmd.cursor.batchSize;
  133. } else if (options.cursor && options.cursor.batchSize) {
  134. batchSize = options.cursor.batchSize;
  135. } else if (typeof options.batchSize === 'number') {
  136. batchSize = options.batchSize;
  137. }
  138. // Set the batchSize
  139. this.setCursorBatchSize(batchSize);
  140. }
  141. get readPreference() {
  142. if (this.operation) {
  143. return this.operation.readPreference;
  144. }
  145. return this.options.readPreference;
  146. }
  147. get sortValue() {
  148. return this.cmd.sort;
  149. }
  150. _initializeCursor(callback) {
  151. if (this.operation && this.operation.session != null) {
  152. this.cursorState.session = this.operation.session;
  153. } else {
  154. // implicitly create a session if one has not been provided
  155. if (
  156. !this.s.explicitlyIgnoreSession &&
  157. !this.cursorState.session &&
  158. this.topology.hasSessionSupport()
  159. ) {
  160. this.cursorState.session = this.topology.startSession({ owner: this });
  161. if (this.operation) {
  162. this.operation.session = this.cursorState.session;
  163. }
  164. }
  165. }
  166. super._initializeCursor(callback);
  167. }
  168. /**
  169. * Check if there is any document still available in the cursor
  170. * @method
  171. * @param {Cursor~resultCallback} [callback] The result callback.
  172. * @throws {MongoError}
  173. * @return {Promise} returns Promise if no callback passed
  174. */
  175. hasNext(callback) {
  176. if (this.s.state === CursorState.CLOSED || (this.isDead && this.isDead())) {
  177. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  178. }
  179. return maybePromise(this, callback, cb => {
  180. const cursor = this;
  181. if (cursor.isNotified()) {
  182. return cb(null, false);
  183. }
  184. cursor._next((err, doc) => {
  185. if (err) return cb(err);
  186. if (doc == null || cursor.s.state === Cursor.CLOSED || cursor.isDead()) {
  187. return cb(null, false);
  188. }
  189. cursor.s.state = CursorState.OPEN;
  190. // NODE-2482: merge this into the core cursor implementation
  191. cursor.cursorState.cursorIndex--;
  192. if (cursor.cursorState.limit > 0) {
  193. cursor.cursorState.currentLimit--;
  194. }
  195. cb(null, true);
  196. });
  197. });
  198. }
  199. /**
  200. * Get the next available document from the cursor, returns null if no more documents are available.
  201. * @method
  202. * @param {Cursor~resultCallback} [callback] The result callback.
  203. * @throws {MongoError}
  204. * @return {Promise} returns Promise if no callback passed
  205. */
  206. next(callback) {
  207. return maybePromise(this, callback, cb => {
  208. const cursor = this;
  209. if (cursor.s.state === CursorState.CLOSED || (cursor.isDead && cursor.isDead())) {
  210. cb(MongoError.create({ message: 'Cursor is closed', driver: true }));
  211. return;
  212. }
  213. if (cursor.s.state === CursorState.INIT && cursor.cmd.sort) {
  214. try {
  215. cursor.cmd.sort = formattedOrderClause(cursor.cmd.sort);
  216. } catch (err) {
  217. return cb(err);
  218. }
  219. }
  220. cursor._next((err, doc) => {
  221. if (err) return cb(err);
  222. cursor.s.state = CursorState.OPEN;
  223. cb(null, doc);
  224. });
  225. });
  226. }
  227. /**
  228. * Set the cursor query
  229. * @method
  230. * @param {object} filter The filter object used for the cursor.
  231. * @return {Cursor}
  232. */
  233. filter(filter) {
  234. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  235. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  236. }
  237. this.cmd.query = filter;
  238. return this;
  239. }
  240. /**
  241. * Set the cursor maxScan
  242. * @method
  243. * @param {object} maxScan Constrains the query to only scan the specified number of documents when fulfilling the query
  244. * @deprecated as of MongoDB 4.0
  245. * @return {Cursor}
  246. */
  247. maxScan(maxScan) {
  248. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  249. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  250. }
  251. this.cmd.maxScan = maxScan;
  252. return this;
  253. }
  254. /**
  255. * Set the cursor hint
  256. * @method
  257. * @param {object} hint If specified, then the query system will only consider plans using the hinted index.
  258. * @return {Cursor}
  259. */
  260. hint(hint) {
  261. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  262. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  263. }
  264. this.cmd.hint = hint;
  265. return this;
  266. }
  267. /**
  268. * Set the cursor min
  269. * @method
  270. * @param {object} min Specify a $min value to specify the inclusive lower bound for a specific index in order to constrain the results of find(). The $min specifies the lower bound for all keys of a specific index in order.
  271. * @return {Cursor}
  272. */
  273. min(min) {
  274. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  275. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  276. }
  277. this.cmd.min = min;
  278. return this;
  279. }
  280. /**
  281. * Set the cursor max
  282. * @method
  283. * @param {object} max Specify a $max value to specify the exclusive upper bound for a specific index in order to constrain the results of find(). The $max specifies the upper bound for all keys of a specific index in order.
  284. * @return {Cursor}
  285. */
  286. max(max) {
  287. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  288. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  289. }
  290. this.cmd.max = max;
  291. return this;
  292. }
  293. /**
  294. * Set the cursor returnKey. If set to true, modifies the cursor to only return the index field or fields for the results of the query, rather than documents. If set to true and the query does not use an index to perform the read operation, the returned documents will not contain any fields.
  295. * @method
  296. * @param {bool} returnKey the returnKey value.
  297. * @return {Cursor}
  298. */
  299. returnKey(value) {
  300. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  301. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  302. }
  303. this.cmd.returnKey = value;
  304. return this;
  305. }
  306. /**
  307. * Set the cursor showRecordId
  308. * @method
  309. * @param {object} showRecordId The $showDiskLoc option has now been deprecated and replaced with the showRecordId field. $showDiskLoc will still be accepted for OP_QUERY stye find.
  310. * @return {Cursor}
  311. */
  312. showRecordId(value) {
  313. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  314. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  315. }
  316. this.cmd.showDiskLoc = value;
  317. return this;
  318. }
  319. /**
  320. * Set the cursor snapshot
  321. * @method
  322. * @param {object} snapshot The $snapshot operator prevents the cursor from returning a document more than once because an intervening write operation results in a move of the document.
  323. * @deprecated as of MongoDB 4.0
  324. * @return {Cursor}
  325. */
  326. snapshot(value) {
  327. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  328. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  329. }
  330. this.cmd.snapshot = value;
  331. return this;
  332. }
  333. /**
  334. * Set a node.js specific cursor option
  335. * @method
  336. * @param {string} field The cursor option to set ['numberOfRetries', 'tailableRetryInterval'].
  337. * @param {object} value The field value.
  338. * @throws {MongoError}
  339. * @return {Cursor}
  340. */
  341. setCursorOption(field, value) {
  342. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  343. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  344. }
  345. if (fields.indexOf(field) === -1) {
  346. throw MongoError.create({
  347. message: `option ${field} is not a supported option ${fields}`,
  348. driver: true
  349. });
  350. }
  351. this.s[field] = value;
  352. if (field === 'numberOfRetries') this.s.currentNumberOfRetries = value;
  353. return this;
  354. }
  355. /**
  356. * Add a cursor flag to the cursor
  357. * @method
  358. * @param {string} flag The flag to set, must be one of following ['tailable', 'oplogReplay', 'noCursorTimeout', 'awaitData', 'partial'].
  359. * @param {boolean} value The flag boolean value.
  360. * @throws {MongoError}
  361. * @return {Cursor}
  362. */
  363. addCursorFlag(flag, value) {
  364. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  365. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  366. }
  367. if (flags.indexOf(flag) === -1) {
  368. throw MongoError.create({
  369. message: `flag ${flag} is not a supported flag ${flags}`,
  370. driver: true
  371. });
  372. }
  373. if (typeof value !== 'boolean') {
  374. throw MongoError.create({ message: `flag ${flag} must be a boolean value`, driver: true });
  375. }
  376. this.cmd[flag] = value;
  377. return this;
  378. }
  379. /**
  380. * Add a query modifier to the cursor query
  381. * @method
  382. * @param {string} name The query modifier (must start with $, such as $orderby etc)
  383. * @param {string|boolean|number} value The modifier value.
  384. * @throws {MongoError}
  385. * @return {Cursor}
  386. */
  387. addQueryModifier(name, value) {
  388. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  389. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  390. }
  391. if (name[0] !== '$') {
  392. throw MongoError.create({ message: `${name} is not a valid query modifier`, driver: true });
  393. }
  394. // Strip of the $
  395. const field = name.substr(1);
  396. // Set on the command
  397. this.cmd[field] = value;
  398. // Deal with the special case for sort
  399. if (field === 'orderby') this.cmd.sort = this.cmd[field];
  400. return this;
  401. }
  402. /**
  403. * Add a comment to the cursor query allowing for tracking the comment in the log.
  404. * @method
  405. * @param {string} value The comment attached to this query.
  406. * @throws {MongoError}
  407. * @return {Cursor}
  408. */
  409. comment(value) {
  410. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  411. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  412. }
  413. this.cmd.comment = value;
  414. return this;
  415. }
  416. /**
  417. * Set a maxAwaitTimeMS on a tailing cursor query to allow to customize the timeout value for the option awaitData (Only supported on MongoDB 3.2 or higher, ignored otherwise)
  418. * @method
  419. * @param {number} value Number of milliseconds to wait before aborting the tailed query.
  420. * @throws {MongoError}
  421. * @return {Cursor}
  422. */
  423. maxAwaitTimeMS(value) {
  424. if (typeof value !== 'number') {
  425. throw MongoError.create({ message: 'maxAwaitTimeMS must be a number', driver: true });
  426. }
  427. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  428. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  429. }
  430. this.cmd.maxAwaitTimeMS = value;
  431. return this;
  432. }
  433. /**
  434. * Set a maxTimeMS on the cursor query, allowing for hard timeout limits on queries (Only supported on MongoDB 2.6 or higher)
  435. * @method
  436. * @param {number} value Number of milliseconds to wait before aborting the query.
  437. * @throws {MongoError}
  438. * @return {Cursor}
  439. */
  440. maxTimeMS(value) {
  441. if (typeof value !== 'number') {
  442. throw MongoError.create({ message: 'maxTimeMS must be a number', driver: true });
  443. }
  444. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  445. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  446. }
  447. this.cmd.maxTimeMS = value;
  448. return this;
  449. }
  450. /**
  451. * Sets a field projection for the query.
  452. * @method
  453. * @param {object} value The field projection object.
  454. * @throws {MongoError}
  455. * @return {Cursor}
  456. */
  457. project(value) {
  458. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  459. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  460. }
  461. this.cmd.fields = value;
  462. return this;
  463. }
  464. /**
  465. * Sets the sort order of the cursor query.
  466. * @method
  467. * @param {(string|array|object)} keyOrList The key or keys set for the sort.
  468. * @param {number} [direction] The direction of the sorting (1 or -1).
  469. * @throws {MongoError}
  470. * @return {Cursor}
  471. */
  472. sort(keyOrList, direction) {
  473. if (this.options.tailable) {
  474. throw MongoError.create({ message: "Tailable cursor doesn't support sorting", driver: true });
  475. }
  476. if (this.s.state === CursorState.CLOSED || this.s.state === CursorState.OPEN || this.isDead()) {
  477. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  478. }
  479. let order = keyOrList;
  480. // We have an array of arrays, we need to preserve the order of the sort
  481. // so we will us a Map
  482. if (Array.isArray(order) && Array.isArray(order[0])) {
  483. order = new Map(
  484. order.map(x => {
  485. const value = [x[0], null];
  486. if (x[1] === 'asc') {
  487. value[1] = 1;
  488. } else if (x[1] === 'desc') {
  489. value[1] = -1;
  490. } else if (x[1] === 1 || x[1] === -1 || x[1].$meta) {
  491. value[1] = x[1];
  492. } else {
  493. throw new MongoError(
  494. "Illegal sort clause, must be of the form [['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]"
  495. );
  496. }
  497. return value;
  498. })
  499. );
  500. }
  501. if (direction != null) {
  502. order = [[keyOrList, direction]];
  503. }
  504. this.cmd.sort = order;
  505. return this;
  506. }
  507. /**
  508. * Set the batch size for the cursor.
  509. * @method
  510. * @param {number} value The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/find/|find command documentation}.
  511. * @throws {MongoError}
  512. * @return {Cursor}
  513. */
  514. batchSize(value) {
  515. if (this.options.tailable) {
  516. throw MongoError.create({
  517. message: "Tailable cursor doesn't support batchSize",
  518. driver: true
  519. });
  520. }
  521. if (this.s.state === CursorState.CLOSED || this.isDead()) {
  522. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  523. }
  524. if (typeof value !== 'number') {
  525. throw MongoError.create({ message: 'batchSize requires an integer', driver: true });
  526. }
  527. this.cmd.batchSize = value;
  528. this.setCursorBatchSize(value);
  529. return this;
  530. }
  531. /**
  532. * Set the collation options for the cursor.
  533. * @method
  534. * @param {object} value The cursor collation options (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
  535. * @throws {MongoError}
  536. * @return {Cursor}
  537. */
  538. collation(value) {
  539. this.cmd.collation = value;
  540. return this;
  541. }
  542. /**
  543. * Set the limit for the cursor.
  544. * @method
  545. * @param {number} value The limit for the cursor query.
  546. * @throws {MongoError}
  547. * @return {Cursor}
  548. */
  549. limit(value) {
  550. if (this.options.tailable) {
  551. throw MongoError.create({ message: "Tailable cursor doesn't support limit", driver: true });
  552. }
  553. if (this.s.state === CursorState.OPEN || this.s.state === CursorState.CLOSED || this.isDead()) {
  554. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  555. }
  556. if (typeof value !== 'number') {
  557. throw MongoError.create({ message: 'limit requires an integer', driver: true });
  558. }
  559. this.cmd.limit = value;
  560. this.setCursorLimit(value);
  561. return this;
  562. }
  563. /**
  564. * Set the skip for the cursor.
  565. * @method
  566. * @param {number} value The skip for the cursor query.
  567. * @throws {MongoError}
  568. * @return {Cursor}
  569. */
  570. skip(value) {
  571. if (this.options.tailable) {
  572. throw MongoError.create({ message: "Tailable cursor doesn't support skip", driver: true });
  573. }
  574. if (this.s.state === CursorState.OPEN || this.s.state === CursorState.CLOSED || this.isDead()) {
  575. throw MongoError.create({ message: 'Cursor is closed', driver: true });
  576. }
  577. if (typeof value !== 'number') {
  578. throw MongoError.create({ message: 'skip requires an integer', driver: true });
  579. }
  580. this.cmd.skip = value;
  581. this.setCursorSkip(value);
  582. return this;
  583. }
  584. /**
  585. * The callback format for results
  586. * @callback Cursor~resultCallback
  587. * @param {MongoError} error An error instance representing the error during the execution.
  588. * @param {(object|null|boolean)} result The result object if the command was executed successfully.
  589. */
  590. /**
  591. * Clone the cursor
  592. * @function external:CoreCursor#clone
  593. * @return {Cursor}
  594. */
  595. /**
  596. * Resets the cursor
  597. * @function external:CoreCursor#rewind
  598. * @return {null}
  599. */
  600. /**
  601. * Iterates over all the documents for this cursor. As with **{cursor.toArray}**,
  602. * not all of the elements will be iterated if this cursor had been previously accessed.
  603. * In that case, **{cursor.rewind}** can be used to reset the cursor. However, unlike
  604. * **{cursor.toArray}**, the cursor will only hold a maximum of batch size elements
  605. * at any given time if batch size is specified. Otherwise, the caller is responsible
  606. * for making sure that the entire result can fit the memory.
  607. * @method
  608. * @deprecated
  609. * @param {Cursor~resultCallback} callback The result callback.
  610. * @throws {MongoError}
  611. * @return {null}
  612. */
  613. each(callback) {
  614. // Rewind cursor state
  615. this.rewind();
  616. // Set current cursor to INIT
  617. this.s.state = CursorState.INIT;
  618. // Run the query
  619. each(this, callback);
  620. }
  621. /**
  622. * The callback format for the forEach iterator method
  623. * @callback Cursor~iteratorCallback
  624. * @param {Object} doc An emitted document for the iterator
  625. */
  626. /**
  627. * The callback error format for the forEach iterator method
  628. * @callback Cursor~endCallback
  629. * @param {MongoError} error An error instance representing the error during the execution.
  630. */
  631. /**
  632. * Iterates over all the documents for this cursor using the iterator, callback pattern.
  633. * @method
  634. * @param {Cursor~iteratorCallback} iterator The iteration callback.
  635. * @param {Cursor~endCallback} callback The end callback.
  636. * @throws {MongoError}
  637. * @return {Promise} if no callback supplied
  638. */
  639. forEach(iterator, callback) {
  640. // Rewind cursor state
  641. this.rewind();
  642. // Set current cursor to INIT
  643. this.s.state = CursorState.INIT;
  644. if (typeof callback === 'function') {
  645. each(this, (err, doc) => {
  646. if (err) {
  647. callback(err);
  648. return false;
  649. }
  650. if (doc != null) {
  651. iterator(doc);
  652. return true;
  653. }
  654. if (doc == null && callback) {
  655. const internalCallback = callback;
  656. callback = null;
  657. internalCallback(null);
  658. return false;
  659. }
  660. });
  661. } else {
  662. return new this.s.promiseLibrary((fulfill, reject) => {
  663. each(this, (err, doc) => {
  664. if (err) {
  665. reject(err);
  666. return false;
  667. } else if (doc == null) {
  668. fulfill(null);
  669. return false;
  670. } else {
  671. iterator(doc);
  672. return true;
  673. }
  674. });
  675. });
  676. }
  677. }
  678. /**
  679. * Set the ReadPreference for the cursor.
  680. * @method
  681. * @param {(string|ReadPreference)} readPreference The new read preference for the cursor.
  682. * @throws {MongoError}
  683. * @return {Cursor}
  684. */
  685. setReadPreference(readPreference) {
  686. if (this.s.state !== CursorState.INIT) {
  687. throw MongoError.create({
  688. message: 'cannot change cursor readPreference after cursor has been accessed',
  689. driver: true
  690. });
  691. }
  692. if (readPreference instanceof ReadPreference) {
  693. this.options.readPreference = readPreference;
  694. } else if (typeof readPreference === 'string') {
  695. this.options.readPreference = new ReadPreference(readPreference);
  696. } else {
  697. throw new TypeError('Invalid read preference: ' + readPreference);
  698. }
  699. return this;
  700. }
  701. /**
  702. * The callback format for results
  703. * @callback Cursor~toArrayResultCallback
  704. * @param {MongoError} error An error instance representing the error during the execution.
  705. * @param {object[]} documents All the documents the satisfy the cursor.
  706. */
  707. /**
  708. * Returns an array of documents. The caller is responsible for making sure that there
  709. * is enough memory to store the results. Note that the array only contains partial
  710. * results when this cursor had been previously accessed. In that case,
  711. * cursor.rewind() can be used to reset the cursor.
  712. * @method
  713. * @param {Cursor~toArrayResultCallback} [callback] The result callback.
  714. * @throws {MongoError}
  715. * @return {Promise} returns Promise if no callback passed
  716. */
  717. toArray(callback) {
  718. if (this.options.tailable) {
  719. throw MongoError.create({
  720. message: 'Tailable cursor cannot be converted to array',
  721. driver: true
  722. });
  723. }
  724. return maybePromise(this, callback, cb => {
  725. const cursor = this;
  726. const items = [];
  727. // Reset cursor
  728. cursor.rewind();
  729. cursor.s.state = CursorState.INIT;
  730. // Fetch all the documents
  731. const fetchDocs = () => {
  732. cursor._next((err, doc) => {
  733. if (err) {
  734. return cursor._endSession
  735. ? cursor._endSession(() => handleCallback(cb, err))
  736. : handleCallback(cb, err);
  737. }
  738. if (doc == null) {
  739. return cursor.close({ skipKillCursors: true }, () => handleCallback(cb, null, items));
  740. }
  741. // Add doc to items
  742. items.push(doc);
  743. // Get all buffered objects
  744. if (cursor.bufferedCount() > 0) {
  745. let docs = cursor.readBufferedDocuments(cursor.bufferedCount());
  746. Array.prototype.push.apply(items, docs);
  747. }
  748. // Attempt a fetch
  749. fetchDocs();
  750. });
  751. };
  752. fetchDocs();
  753. });
  754. }
  755. /**
  756. * The callback format for results
  757. * @callback Cursor~countResultCallback
  758. * @param {MongoError} error An error instance representing the error during the execution.
  759. * @param {number} count The count of documents.
  760. */
  761. /**
  762. * Get the count of documents for this cursor
  763. * @method
  764. * @param {boolean} [applySkipLimit=true] Should the count command apply limit and skip settings on the cursor or in the passed in options.
  765. * @param {object} [options] Optional settings.
  766. * @param {number} [options.skip] The number of documents to skip.
  767. * @param {number} [options.limit] The maximum amounts to count before aborting.
  768. * @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
  769. * @param {string} [options.hint] An index name hint for the query.
  770. * @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  771. * @param {Cursor~countResultCallback} [callback] The result callback.
  772. * @return {Promise} returns Promise if no callback passed
  773. */
  774. count(applySkipLimit, opts, callback) {
  775. if (this.cmd.query == null)
  776. throw MongoError.create({
  777. message: 'count can only be used with find command',
  778. driver: true
  779. });
  780. if (typeof opts === 'function') (callback = opts), (opts = {});
  781. opts = opts || {};
  782. if (typeof applySkipLimit === 'function') {
  783. callback = applySkipLimit;
  784. applySkipLimit = true;
  785. }
  786. if (this.cursorState.session) {
  787. opts = Object.assign({}, opts, { session: this.cursorState.session });
  788. }
  789. const countOperation = new CountOperation(this, applySkipLimit, opts);
  790. return executeOperation(this.topology, countOperation, callback);
  791. }
  792. /**
  793. * Close the cursor, sending a KillCursor command and emitting close.
  794. * @method
  795. * @param {object} [options] Optional settings.
  796. * @param {boolean} [options.skipKillCursors] Bypass calling killCursors when closing the cursor.
  797. * @param {Cursor~resultCallback} [callback] The result callback.
  798. * @return {Promise} returns Promise if no callback passed
  799. */
  800. close(options, callback) {
  801. if (typeof options === 'function') (callback = options), (options = {});
  802. options = Object.assign({}, { skipKillCursors: false }, options);
  803. this.s.state = CursorState.CLOSED;
  804. if (!options.skipKillCursors) {
  805. // Kill the cursor
  806. this.kill();
  807. }
  808. const completeClose = () => {
  809. // Emit the close event for the cursor
  810. this.emit('close');
  811. // Callback if provided
  812. if (typeof callback === 'function') {
  813. return handleCallback(callback, null, this);
  814. }
  815. // Return a Promise
  816. return new this.s.promiseLibrary(resolve => {
  817. resolve();
  818. });
  819. };
  820. if (this.cursorState.session) {
  821. if (typeof callback === 'function') {
  822. return this._endSession(() => completeClose());
  823. }
  824. return new this.s.promiseLibrary(resolve => {
  825. this._endSession(() => completeClose().then(resolve));
  826. });
  827. }
  828. return completeClose();
  829. }
  830. /**
  831. * Map all documents using the provided function
  832. * @method
  833. * @param {function} [transform] The mapping transformation method.
  834. * @return {Cursor}
  835. */
  836. map(transform) {
  837. if (this.cursorState.transforms && this.cursorState.transforms.doc) {
  838. const oldTransform = this.cursorState.transforms.doc;
  839. this.cursorState.transforms.doc = doc => {
  840. return transform(oldTransform(doc));
  841. };
  842. } else {
  843. this.cursorState.transforms = { doc: transform };
  844. }
  845. return this;
  846. }
  847. /**
  848. * Is the cursor closed
  849. * @method
  850. * @return {boolean}
  851. */
  852. isClosed() {
  853. return this.isDead();
  854. }
  855. destroy(err) {
  856. if (err) this.emit('error', err);
  857. this.pause();
  858. this.close();
  859. }
  860. /**
  861. * Return a modified Readable stream including a possible transform method.
  862. * @method
  863. * @param {object} [options] Optional settings.
  864. * @param {function} [options.transform] A transformation method applied to each document emitted by the stream.
  865. * @return {Cursor}
  866. * TODO: replace this method with transformStream in next major release
  867. */
  868. stream(options) {
  869. this.cursorState.streamOptions = options || {};
  870. return this;
  871. }
  872. /**
  873. * Return a modified Readable stream that applies a given transform function, if supplied. If none supplied,
  874. * returns a stream of unmodified docs.
  875. * @method
  876. * @param {object} [options] Optional settings.
  877. * @param {function} [options.transform] A transformation method applied to each document emitted by the stream.
  878. * @return {stream}
  879. */
  880. transformStream(options) {
  881. const streamOptions = options || {};
  882. if (typeof streamOptions.transform === 'function') {
  883. const stream = new Transform({
  884. objectMode: true,
  885. transform: function(chunk, encoding, callback) {
  886. this.push(streamOptions.transform(chunk));
  887. callback();
  888. }
  889. });
  890. return this.pipe(stream);
  891. }
  892. return this.pipe(new PassThrough({ objectMode: true }));
  893. }
  894. /**
  895. * Execute the explain for the cursor
  896. * @method
  897. * @param {Cursor~resultCallback} [callback] The result callback.
  898. * @return {Promise} returns Promise if no callback passed
  899. */
  900. explain(callback) {
  901. // NOTE: the next line includes a special case for operations which do not
  902. // subclass `CommandOperationV2`. To be removed asap.
  903. if (this.operation && this.operation.cmd == null) {
  904. this.operation.options.explain = true;
  905. this.operation.fullResponse = false;
  906. return executeOperation(this.topology, this.operation, callback);
  907. }
  908. this.cmd.explain = true;
  909. // Do we have a readConcern
  910. if (this.cmd.readConcern) {
  911. delete this.cmd['readConcern'];
  912. }
  913. return maybePromise(this, callback, cb => {
  914. CoreCursor.prototype._next.apply(this, [cb]);
  915. });
  916. }
  917. /**
  918. * Return the cursor logger
  919. * @method
  920. * @return {Logger} return the cursor logger
  921. * @ignore
  922. */
  923. getLogger() {
  924. return this.logger;
  925. }
  926. }
  927. /**
  928. * Cursor stream data event, fired for each document in the cursor.
  929. *
  930. * @event Cursor#data
  931. * @type {object}
  932. */
  933. /**
  934. * Cursor stream end event
  935. *
  936. * @event Cursor#end
  937. * @type {null}
  938. */
  939. /**
  940. * Cursor stream close event
  941. *
  942. * @event Cursor#close
  943. * @type {null}
  944. */
  945. /**
  946. * Cursor stream readable event
  947. *
  948. * @event Cursor#readable
  949. * @type {null}
  950. */
  951. // aliases
  952. Cursor.prototype.maxTimeMs = Cursor.prototype.maxTimeMS;
  953. // deprecated methods
  954. deprecate(Cursor.prototype.each, 'Cursor.each is deprecated. Use Cursor.forEach instead.');
  955. deprecate(
  956. Cursor.prototype.maxScan,
  957. 'Cursor.maxScan is deprecated, and will be removed in a later version'
  958. );
  959. deprecate(
  960. Cursor.prototype.snapshot,
  961. 'Cursor Snapshot is deprecated, and will be removed in a later version'
  962. );
  963. /**
  964. * The read() method pulls some data out of the internal buffer and returns it. If there is no data available, then it will return null.
  965. * @function external:Readable#read
  966. * @param {number} size Optional argument to specify how much data to read.
  967. * @return {(String | Buffer | null)}
  968. */
  969. /**
  970. * Call this function to cause the stream to return strings of the specified encoding instead of Buffer objects.
  971. * @function external:Readable#setEncoding
  972. * @param {string} encoding The encoding to use.
  973. * @return {null}
  974. */
  975. /**
  976. * This method will cause the readable stream to resume emitting data events.
  977. * @function external:Readable#resume
  978. * @return {null}
  979. */
  980. /**
  981. * This method will cause a stream in flowing-mode to stop emitting data events. Any data that becomes available will remain in the internal buffer.
  982. * @function external:Readable#pause
  983. * @return {null}
  984. */
  985. /**
  986. * This method pulls all the data out of a readable stream, and writes it to the supplied destination, automatically managing the flow so that the destination is not overwhelmed by a fast readable stream.
  987. * @function external:Readable#pipe
  988. * @param {Writable} destination The destination for writing data
  989. * @param {object} [options] Pipe options
  990. * @return {null}
  991. */
  992. /**
  993. * This method will remove the hooks set up for a previous pipe() call.
  994. * @function external:Readable#unpipe
  995. * @param {Writable} [destination] The destination for writing data
  996. * @return {null}
  997. */
  998. /**
  999. * This is useful in certain cases where a stream is being consumed by a parser, which needs to "un-consume" some data that it has optimistically pulled out of the source, so that the stream can be passed on to some other party.
  1000. * @function external:Readable#unshift
  1001. * @param {(Buffer|string)} chunk Chunk of data to unshift onto the read queue.
  1002. * @return {null}
  1003. */
  1004. /**
  1005. * Versions of Node prior to v0.10 had streams that did not implement the entire Streams API as it is today. (See "Compatibility" below for more information.)
  1006. * @function external:Readable#wrap
  1007. * @param {Stream} stream An "old style" readable stream.
  1008. * @return {null}
  1009. */
  1010. module.exports = Cursor;