cursor.js 40 KB

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