cursor.js 40.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
'use strict';

var inherits = require('util').inherits,
  f = require('util').format,
  formattedOrderClause = require('./utils').formattedOrderClause,
  handleCallback = require('./utils').handleCallback,
  ReadPreference = require('mongodb-core').ReadPreference,
  MongoError = require('mongodb-core').MongoError,
  Readable = require('stream').Readable,
  Define = require('./metadata'),
  CoreCursor = require('mongodb-core').Cursor,
  Map = require('mongodb-core').BSON.Map,
  executeOperation = require('./utils').executeOperation;

/**
 * @fileOverview The **Cursor** class is an internal class that embodies a cursor on MongoDB
 * allowing for iteration over the results returned from the underlying query. It supports
 * one by one document iteration, conversion to an array or can be iterated as a Node 4.X
 * or higher stream
 *
 * **CURSORS Cannot directly be instantiated**
 * @example
 * const MongoClient = require('mongodb').MongoClient;
 * const test = require('assert');
 * // Connection url
 * const url = 'mongodb://localhost:27017';
 * // Database Name
 * const dbName = 'test';
 * // Connect using MongoClient
 * MongoClient.connect(url, function(err, client) {
 *   // Create a collection we want to drop later
 *   const col = client.db(dbName).collection('createIndexExample1');
 *   // Insert a bunch of documents
 *   col.insert([{a:1, b:1}
 *     , {a:2, b:2}, {a:3, b:3}
 *     , {a:4, b:4}], {w:1}, function(err, result) {
 *     test.equal(null, err);
 *     // Show that duplicate records got dropped
 *     col.find({}).toArray(function(err, items) {
 *       test.equal(null, err);
 *       test.equal(4, items.length);
 *       client.close();
 *     });
 *   });
 * });
 */

/**
 * Namespace provided by the mongodb-core and node.js
 * @external CoreCursor
 * @external Readable
 */

// Flags allowed for cursor
var flags = ['tailable', 'oplogReplay', 'noCursorTimeout', 'awaitData', 'exhaust', 'partial'];
var fields = ['numberOfRetries', 'tailableRetryInterval'];
var push = Array.prototype.push;

/**
 * Creates a new Cursor instance (INTERNAL TYPE, do not instantiate directly)
 * @class Cursor
 * @extends external:CoreCursor
 * @extends external:Readable
 * @property {string} sortValue Cursor query sort setting.
 * @property {boolean} timeout Is Cursor able to time out.
 * @property {ReadPreference} readPreference Get cursor ReadPreference.
 * @fires Cursor#data
 * @fires Cursor#end
 * @fires Cursor#close
 * @fires Cursor#readable
 * @return {Cursor} a Cursor instance.
 * @example
 * Cursor cursor options.
 *
 * collection.find({}).project({a:1})                             // Create a projection of field a
 * collection.find({}).skip(1).limit(10)                          // Skip 1 and limit 10
 * collection.find({}).batchSize(5)                               // Set batchSize on cursor to 5
 * collection.find({}).filter({a:1})                              // Set query on the cursor
 * collection.find({}).comment('add a comment')                   // Add a comment to the query, allowing to correlate queries
 * collection.find({}).addCursorFlag('tailable', true)            // Set cursor as tailable
 * collection.find({}).addCursorFlag('oplogReplay', true)         // Set cursor as oplogReplay
 * collection.find({}).addCursorFlag('noCursorTimeout', true)     // Set cursor as noCursorTimeout
 * collection.find({}).addCursorFlag('awaitData', true)           // Set cursor as awaitData
 * collection.find({}).addCursorFlag('partial', true)             // Set cursor as partial
 * collection.find({}).addQueryModifier('$orderby', {a:1})        // Set $orderby {a:1}
 * collection.find({}).max(10)                                    // Set the cursor maxScan
 * collection.find({}).maxScan(10)                                // Set the cursor maxScan
 * collection.find({}).maxTimeMS(1000)                            // Set the cursor maxTimeMS
 * collection.find({}).min(100)                                   // Set the cursor min
 * collection.find({}).returnKey(10)                              // Set the cursor returnKey
 * collection.find({}).setReadPreference(ReadPreference.PRIMARY)  // Set the cursor readPreference
 * collection.find({}).showRecordId(true)                         // Set the cursor showRecordId
 * collection.find({}).snapshot(true)                             // Set the cursor snapshot
 * collection.find({}).sort([['a', 1]])                           // Sets the sort order of the cursor query
 * collection.find({}).hint('a_1')                                // Set the cursor hint
 *
 * All options are chainable, so one can do the following.
 *
 * collection.find({}).maxTimeMS(1000).maxScan(100).skip(1).toArray(..)
 */
var Cursor = function(bson, ns, cmd, options, topology, topologyOptions) {
  CoreCursor.apply(this, Array.prototype.slice.call(arguments, 0));
  var state = Cursor.INIT;
  var streamOptions = {};

  // Tailable cursor options
  var numberOfRetries = options.numberOfRetries || 5;
  var tailableRetryInterval = options.tailableRetryInterval || 500;
  var currentNumberOfRetries = numberOfRetries;

  // Get the promiseLibrary
  var promiseLibrary = options.promiseLibrary || Promise;

  // Set up
  Readable.call(this, { objectMode: true });

  // Internal cursor state
  this.s = {
    // Tailable cursor options
    numberOfRetries: numberOfRetries,
    tailableRetryInterval: tailableRetryInterval,
    currentNumberOfRetries: currentNumberOfRetries,
    // State
    state: state,
    // Stream options
    streamOptions: streamOptions,
    // BSON
    bson: bson,
    // Namespace
    ns: ns,
    // Command
    cmd: cmd,
    // Options
    options: options,
    // Topology
    topology: topology,
    // Topology options
    topologyOptions: topologyOptions,
    // Promise library
    promiseLibrary: promiseLibrary,
    // Current doc
    currentDoc: null,
    // Optional ClientSession
    session: options.session
  };

  // Translate correctly
  if (this.s.options.noCursorTimeout === true) {
    this.addCursorFlag('noCursorTimeout', true);
  }

  // Set the sort value
  this.sortValue = this.s.cmd.sort;

  // Get the batchSize
  var batchSize =
    cmd.cursor && cmd.cursor.batchSize
      ? cmd.cursor && cmd.cursor.batchSize
      : options.cursor && options.cursor.batchSize ? options.cursor.batchSize : 1000;

  // Set the batchSize
  this.setCursorBatchSize(batchSize);
};

/**
 * Cursor stream data event, fired for each document in the cursor.
 *
 * @event Cursor#data
 * @type {object}
 */

/**
 * Cursor stream end event
 *
 * @event Cursor#end
 * @type {null}
 */

/**
 * Cursor stream close event
 *
 * @event Cursor#close
 * @type {null}
 */

/**
 * Cursor stream readable event
 *
 * @event Cursor#readable
 * @type {null}
 */

// Inherit from Readable
inherits(Cursor, Readable);

// Map core cursor _next method so we can apply mapping
CoreCursor.prototype._next = CoreCursor.prototype.next;

for (var name in CoreCursor.prototype) {
  Cursor.prototype[name] = CoreCursor.prototype[name];
}

var define = (Cursor.define = new Define('Cursor', Cursor, true));

/**
 * Check if there is any document still available in the cursor
 * @method
 * @param {Cursor~resultCallback} [callback] The result callback.
 * @throws {MongoError}
 * @return {Promise} returns Promise if no callback passed
 */
Cursor.prototype.hasNext = function(callback) {
  return executeOperation(this.s.topology, hasNext, [this, callback], {
    skipSessions: true
  });
};

const hasNext = (self, callback) => {
  if (self.s.currentDoc) {
    return callback(null, true);
  }

  nextObject(self, function(err, doc) {
    if (err) return callback(err, null);
    if (self.s.state === Cursor.CLOSED || self.isDead()) return callback(null, false);
    if (!doc) return callback(null, false);
    self.s.currentDoc = doc;
    callback(null, true);
  });
};

define.classMethod('hasNext', { callback: true, promise: true });

/**
 * Get the next available document from the cursor, returns null if no more documents are available.
 * @method
 * @param {Cursor~resultCallback} [callback] The result callback.
 * @throws {MongoError}
 * @return {Promise} returns Promise if no callback passed
 */
Cursor.prototype.next = function(callback) {
  return executeOperation(this.s.topology, next, [this, callback], {
    skipSessions: true
  });
};

const next = (self, callback) => {
  // Return the currentDoc if someone called hasNext first
  if (self.s.currentDoc) {
    var doc = self.s.currentDoc;
    self.s.currentDoc = null;
    return callback(null, doc);
  }

  // Return the next object
  nextObject(self, callback);
};

define.classMethod('next', { callback: true, promise: true });

/**
 * Set the cursor query
 * @method
 * @param {object} filter The filter object used for the cursor.
 * @return {Cursor}
 */
Cursor.prototype.filter = function(filter) {
  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  this.s.cmd.query = filter;
  return this;
};

define.classMethod('filter', { callback: false, promise: false, returns: [Cursor] });

/**
 * Set the cursor maxScan
 * @method
 * @param {object} maxScan Constrains the query to only scan the specified number of documents when fulfilling the query
 * @return {Cursor}
 */
Cursor.prototype.maxScan = function(maxScan) {
  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  this.s.cmd.maxScan = maxScan;
  return this;
};

define.classMethod('maxScan', { callback: false, promise: false, returns: [Cursor] });

/**
 * Set the cursor hint
 * @method
 * @param {object} hint If specified, then the query system will only consider plans using the hinted index.
 * @return {Cursor}
 */
Cursor.prototype.hint = function(hint) {
  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  this.s.cmd.hint = hint;
  return this;
};

define.classMethod('hint', { callback: false, promise: false, returns: [Cursor] });

/**
 * Set the cursor min
 * @method
 * @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.
 * @return {Cursor}
 */
Cursor.prototype.min = function(min) {
  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead())
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  this.s.cmd.min = min;
  return this;
};

define.classMethod('min', { callback: false, promise: false, returns: [Cursor] });

/**
 * Set the cursor max
 * @method
 * @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.
 * @return {Cursor}
 */
Cursor.prototype.max = function(max) {
  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  this.s.cmd.max = max;
  return this;
};

define.classMethod('max', { callback: false, promise: false, returns: [Cursor] });

/**
 * Set the cursor returnKey
 * @method
 * @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:
 * @return {Cursor}
 */
Cursor.prototype.returnKey = function(value) {
  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  this.s.cmd.returnKey = value;
  return this;
};

define.classMethod('returnKey', { callback: false, promise: false, returns: [Cursor] });

/**
 * Set the cursor showRecordId
 * @method
 * @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.
 * @return {Cursor}
 */
Cursor.prototype.showRecordId = function(value) {
  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  this.s.cmd.showDiskLoc = value;
  return this;
};

define.classMethod('showRecordId', { callback: false, promise: false, returns: [Cursor] });

/**
 * Set the cursor snapshot
 * @method
 * @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.
 * @return {Cursor}
 */
Cursor.prototype.snapshot = function(value) {
  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  this.s.cmd.snapshot = value;
  return this;
};

define.classMethod('snapshot', { callback: false, promise: false, returns: [Cursor] });

/**
 * Set a node.js specific cursor option
 * @method
 * @param {string} field The cursor option to set ['numberOfRetries', 'tailableRetryInterval'].
 * @param {object} value The field value.
 * @throws {MongoError}
 * @return {Cursor}
 */
Cursor.prototype.setCursorOption = function(field, value) {
  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  if (fields.indexOf(field) === -1) {
    throw MongoError.create({
      message: f('option %s not a supported option %s', field, fields),
      driver: true
    });
  }

  this.s[field] = value;
  if (field === 'numberOfRetries') this.s.currentNumberOfRetries = value;
  return this;
};

define.classMethod('setCursorOption', { callback: false, promise: false, returns: [Cursor] });

/**
 * Add a cursor flag to the cursor
 * @method
 * @param {string} flag The flag to set, must be one of following ['tailable', 'oplogReplay', 'noCursorTimeout', 'awaitData', 'partial'].
 * @param {boolean} value The flag boolean value.
 * @throws {MongoError}
 * @return {Cursor}
 */
Cursor.prototype.addCursorFlag = function(flag, value) {
  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  if (flags.indexOf(flag) === -1) {
    throw MongoError.create({
      message: f('flag %s not a supported flag %s', flag, flags),
      driver: true
    });
  }

  if (typeof value !== 'boolean') {
    throw MongoError.create({ message: f('flag %s must be a boolean value', flag), driver: true });
  }

  this.s.cmd[flag] = value;
  return this;
};

define.classMethod('addCursorFlag', { callback: false, promise: false, returns: [Cursor] });

/**
 * Add a query modifier to the cursor query
 * @method
 * @param {string} name The query modifier (must start with $, such as $orderby etc)
 * @param {boolean} value The flag boolean value.
 * @throws {MongoError}
 * @return {Cursor}
 */
Cursor.prototype.addQueryModifier = function(name, value) {
  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  if (name[0] !== '$') {
    throw MongoError.create({ message: f('%s is not a valid query modifier'), driver: true });
  }

  // Strip of the $
  var field = name.substr(1);
  // Set on the command
  this.s.cmd[field] = value;
  // Deal with the special case for sort
  if (field === 'orderby') this.s.cmd.sort = this.s.cmd[field];
  return this;
};

define.classMethod('addQueryModifier', { callback: false, promise: false, returns: [Cursor] });

/**
 * Add a comment to the cursor query allowing for tracking the comment in the log.
 * @method
 * @param {string} value The comment attached to this query.
 * @throws {MongoError}
 * @return {Cursor}
 */
Cursor.prototype.comment = function(value) {
  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  this.s.cmd.comment = value;
  return this;
};

define.classMethod('comment', { callback: false, promise: false, returns: [Cursor] });

/**
 * 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)
 * @method
 * @param {number} value Number of milliseconds to wait before aborting the tailed query.
 * @throws {MongoError}
 * @return {Cursor}
 */
Cursor.prototype.maxAwaitTimeMS = function(value) {
  if (typeof value !== 'number') {
    throw MongoError.create({ message: 'maxAwaitTimeMS must be a number', driver: true });
  }

  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  this.s.cmd.maxAwaitTimeMS = value;
  return this;
};

define.classMethod('maxAwaitTimeMS', { callback: false, promise: false, returns: [Cursor] });

/**
 * Set a maxTimeMS on the cursor query, allowing for hard timeout limits on queries (Only supported on MongoDB 2.6 or higher)
 * @method
 * @param {number} value Number of milliseconds to wait before aborting the query.
 * @throws {MongoError}
 * @return {Cursor}
 */
Cursor.prototype.maxTimeMS = function(value) {
  if (typeof value !== 'number') {
    throw MongoError.create({ message: 'maxTimeMS must be a number', driver: true });
  }

  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  this.s.cmd.maxTimeMS = value;
  return this;
};

define.classMethod('maxTimeMS', { callback: false, promise: false, returns: [Cursor] });

Cursor.prototype.maxTimeMs = Cursor.prototype.maxTimeMS;

define.classMethod('maxTimeMs', { callback: false, promise: false, returns: [Cursor] });

/**
 * Sets a field projection for the query.
 * @method
 * @param {object} value The field projection object.
 * @throws {MongoError}
 * @return {Cursor}
 */
Cursor.prototype.project = function(value) {
  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  this.s.cmd.fields = value;
  return this;
};

define.classMethod('project', { callback: false, promise: false, returns: [Cursor] });

/**
 * Sets the sort order of the cursor query.
 * @method
 * @param {(string|array|object)} keyOrList The key or keys set for the sort.
 * @param {number} [direction] The direction of the sorting (1 or -1).
 * @throws {MongoError}
 * @return {Cursor}
 */
Cursor.prototype.sort = function(keyOrList, direction) {
  if (this.s.options.tailable) {
    throw MongoError.create({ message: "Tailable cursor doesn't support sorting", driver: true });
  }

  if (this.s.state === Cursor.CLOSED || this.s.state === Cursor.OPEN || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  var order = keyOrList;

  // We have an array of arrays, we need to preserve the order of the sort
  // so we will us a Map
  if (Array.isArray(order) && Array.isArray(order[0])) {
    order = new Map(
      order.map(function(x) {
        var value = [x[0], null];
        if (x[1] === 'asc') {
          value[1] = 1;
        } else if (x[1] === 'desc') {
          value[1] = -1;
        } else if (x[1] === 1 || x[1] === -1) {
          value[1] = x[1];
        } else {
          throw new MongoError(
            "Illegal sort clause, must be of the form [['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]"
          );
        }

        return value;
      })
    );
  }

  if (direction != null) {
    order = [[keyOrList, direction]];
  }

  this.s.cmd.sort = order;
  this.sortValue = order;
  return this;
};

define.classMethod('sort', { callback: false, promise: false, returns: [Cursor] });

/**
 * Set the batch size for the cursor.
 * @method
 * @param {number} value The batchSize for the cursor.
 * @throws {MongoError}
 * @return {Cursor}
 */
Cursor.prototype.batchSize = function(value) {
  if (this.s.options.tailable) {
    throw MongoError.create({ message: "Tailable cursor doesn't support batchSize", driver: true });
  }

  if (this.s.state === Cursor.CLOSED || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  if (typeof value !== 'number') {
    throw MongoError.create({ message: 'batchSize requires an integer', driver: true });
  }

  this.s.cmd.batchSize = value;
  this.setCursorBatchSize(value);
  return this;
};

define.classMethod('batchSize', { callback: false, promise: false, returns: [Cursor] });

/**
 * Set the collation options for the cursor.
 * @method
 * @param {object} value The cursor collation options (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
 * @throws {MongoError}
 * @return {Cursor}
 */
Cursor.prototype.collation = function(value) {
  this.s.cmd.collation = value;
  return this;
};

define.classMethod('collation', { callback: false, promise: false, returns: [Cursor] });

/**
 * Set the limit for the cursor.
 * @method
 * @param {number} value The limit for the cursor query.
 * @throws {MongoError}
 * @return {Cursor}
 */
Cursor.prototype.limit = function(value) {
  if (this.s.options.tailable) {
    throw MongoError.create({ message: "Tailable cursor doesn't support limit", driver: true });
  }

  if (this.s.state === Cursor.OPEN || this.s.state === Cursor.CLOSED || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  if (typeof value !== 'number') {
    throw MongoError.create({ message: 'limit requires an integer', driver: true });
  }

  this.s.cmd.limit = value;
  // this.cursorLimit = value;
  this.setCursorLimit(value);
  return this;
};

define.classMethod('limit', { callback: false, promise: false, returns: [Cursor] });

/**
 * Set the skip for the cursor.
 * @method
 * @param {number} value The skip for the cursor query.
 * @throws {MongoError}
 * @return {Cursor}
 */
Cursor.prototype.skip = function(value) {
  if (this.s.options.tailable) {
    throw MongoError.create({ message: "Tailable cursor doesn't support skip", driver: true });
  }

  if (this.s.state === Cursor.OPEN || this.s.state === Cursor.CLOSED || this.isDead()) {
    throw MongoError.create({ message: 'Cursor is closed', driver: true });
  }

  if (typeof value !== 'number') {
    throw MongoError.create({ message: 'skip requires an integer', driver: true });
  }

  this.s.cmd.skip = value;
  this.setCursorSkip(value);
  return this;
};

define.classMethod('skip', { callback: false, promise: false, returns: [Cursor] });

/**
 * The callback format for results
 * @callback Cursor~resultCallback
 * @param {MongoError} error An error instance representing the error during the execution.
 * @param {(object|null|boolean)} result The result object if the command was executed successfully.
 */

/**
 * Clone the cursor
 * @function external:CoreCursor#clone
 * @return {Cursor}
 */

/**
 * Resets the cursor
 * @function external:CoreCursor#rewind
 * @return {null}
 */

// Get the next available document from the cursor, returns null if no more documents are available.
var nextObject = function(self, callback) {
  if (self.s.state === Cursor.CLOSED || (self.isDead && self.isDead()))
    return handleCallback(
      callback,
      MongoError.create({ message: 'Cursor is closed', driver: true })
    );
  if (self.s.state === Cursor.INIT && self.s.cmd.sort) {
    try {
      self.s.cmd.sort = formattedOrderClause(self.s.cmd.sort);
    } catch (err) {
      return handleCallback(callback, err);
    }
  }

  // Get the next object
  self._next(function(err, doc) {
    self.s.state = Cursor.OPEN;
    if (err) return handleCallback(callback, err);
    handleCallback(callback, null, doc);
  });
};

// Trampoline emptying the number of retrieved items
// without incurring a nextTick operation
var loop = function(self, callback) {
  // No more items we are done
  if (self.bufferedCount() === 0) return;
  // Get the next document
  self._next(callback);
  // Loop
  return loop;
};

/**
 * Iterates over all the documents for this cursor. As with **{cursor.toArray}**,
 * not all of the elements will be iterated if this cursor had been previouly accessed.
 * In that case, **{cursor.rewind}** can be used to reset the cursor. However, unlike
 * **{cursor.toArray}**, the cursor will only hold a maximum of batch size elements
 * at any given time if batch size is specified. Otherwise, the caller is responsible
 * for making sure that the entire result can fit the memory.
 * @method
 * @deprecated
 * @param {Cursor~resultCallback} callback The result callback.
 * @throws {MongoError}
 * @return {null}
 */
Cursor.prototype.each = function(callback) {
  // Rewind cursor state
  this.rewind();
  // Set current cursor to INIT
  this.s.state = Cursor.INIT;
  // Run the query
  _each(this, callback);
};

define.classMethod('each', { callback: true, promise: false });

// Run the each loop
var _each = function(self, callback) {
  if (!callback) throw MongoError.create({ message: 'callback is mandatory', driver: true });
  if (self.isNotified()) return;
  if (self.s.state === Cursor.CLOSED || self.isDead()) {
    return handleCallback(
      callback,
      MongoError.create({ message: 'Cursor is closed', driver: true })
    );
  }

  if (self.s.state === Cursor.INIT) self.s.state = Cursor.OPEN;

  // Define function to avoid global scope escape
  var fn = null;
  // Trampoline all the entries
  if (self.bufferedCount() > 0) {
    while ((fn = loop(self, callback))) fn(self, callback);
    _each(self, callback);
  } else {
    self.next(function(err, item) {
      if (err) return handleCallback(callback, err);
      if (item == null) {
        return self.close({ skipKillCursors: true }, () => handleCallback(callback, null, null));
      }

      if (handleCallback(callback, null, item) === false) return;
      _each(self, callback);
    });
  }
};

/**
 * The callback format for the forEach iterator method
 * @callback Cursor~iteratorCallback
 * @param {Object} doc An emitted document for the iterator
 */

/**
 * The callback error format for the forEach iterator method
 * @callback Cursor~endCallback
 * @param {MongoError} error An error instance representing the error during the execution.
 */

/**
 * Iterates over all the documents for this cursor using the iterator, callback pattern.
 * @method
 * @param {Cursor~iteratorCallback} iterator The iteration callback.
 * @param {Cursor~endCallback} callback The end callback.
 * @throws {MongoError}
 * @return {null}
 */
Cursor.prototype.forEach = function(iterator, callback) {
  this.each(function(err, doc) {
    if (err) {
      callback(err);
      return false;
    }
    if (doc != null) {
      iterator(doc);
      return true;
    }
    if (doc == null && callback) {
      var internalCallback = callback;
      callback = null;
      internalCallback(null);
      return false;
    }
  });
};

define.classMethod('forEach', { callback: true, promise: false });

/**
 * Set the ReadPreference for the cursor.
 * @method
 * @param {(string|ReadPreference)} readPreference The new read preference for the cursor.
 * @throws {MongoError}
 * @return {Cursor}
 */
Cursor.prototype.setReadPreference = function(readPreference) {
  if (this.s.state !== Cursor.INIT) {
    throw MongoError.create({
      message: 'cannot change cursor readPreference after cursor has been accessed',
      driver: true
    });
  }

  if (readPreference instanceof ReadPreference) {
    this.s.options.readPreference = readPreference;
  } else if (typeof readPreference === 'string') {
    this.s.options.readPreference = new ReadPreference(readPreference);
  } else {
    throw new TypeError('Invalid read preference: ' + readPreference);
  }

  return this;
};

define.classMethod('setReadPreference', { callback: false, promise: false, returns: [Cursor] });

/**
 * The callback format for results
 * @callback Cursor~toArrayResultCallback
 * @param {MongoError} error An error instance representing the error during the execution.
 * @param {object[]} documents All the documents the satisfy the cursor.
 */

/**
 * Returns an array of documents. The caller is responsible for making sure that there
 * is enough memory to store the results. Note that the array only contain partial
 * results when this cursor had been previouly accessed. In that case,
 * cursor.rewind() can be used to reset the cursor.
 * @method
 * @param {Cursor~toArrayResultCallback} [callback] The result callback.
 * @throws {MongoError}
 * @return {Promise} returns Promise if no callback passed
 */
Cursor.prototype.toArray = function(callback) {
  var self = this;
  if (self.s.options.tailable) {
    throw MongoError.create({
      message: 'Tailable cursor cannot be converted to array',
      driver: true
    });
  }

  return executeOperation(this.s.topology, toArray, [this, callback], {
    skipSessions: true
  });
};

var toArray = function(self, callback) {
  var items = [];

  // Reset cursor
  self.rewind();
  self.s.state = Cursor.INIT;

  // Fetch all the documents
  var fetchDocs = function() {
    self._next(function(err, doc) {
      if (err) return handleCallback(callback, err);
      if (doc == null) {
        return self.close({ skipKillCursors: true }, () => handleCallback(callback, null, items));
      }

      // Add doc to items
      items.push(doc);

      // Get all buffered objects
      if (self.bufferedCount() > 0) {
        var docs = self.readBufferedDocuments(self.bufferedCount());

        // Transform the doc if transform method added
        if (self.s.transforms && typeof self.s.transforms.doc === 'function') {
          docs = docs.map(self.s.transforms.doc);
        }

        push.apply(items, docs);
      }

      // Attempt a fetch
      fetchDocs();
    });
  };

  fetchDocs();
};

define.classMethod('toArray', { callback: true, promise: true });

/**
 * The callback format for results
 * @callback Cursor~countResultCallback
 * @param {MongoError} error An error instance representing the error during the execution.
 * @param {number} count The count of documents.
 */

/**
 * Get the count of documents for this cursor
 * @method
 * @param {boolean} [applySkipLimit=true] Should the count command apply limit and skip settings on the cursor or in the passed in options.
 * @param {object} [options=null] Optional settings.
 * @param {number} [options.skip=null] The number of documents to skip.
 * @param {number} [options.limit=null] The maximum amounts to count before aborting.
 * @param {number} [options.maxTimeMS=null] Number of miliseconds to wait before aborting the query.
 * @param {string} [options.hint=null] An index name hint for the query.
 * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
 * @param {Cursor~countResultCallback} [callback] The result callback.
 * @return {Promise} returns Promise if no callback passed
 */
Cursor.prototype.count = function(applySkipLimit, opts, callback) {
  if (this.s.cmd.query == null)
    throw MongoError.create({ message: 'count can only be used with find command', driver: true });
  if (typeof opts === 'function') (callback = opts), (opts = {});
  opts = opts || {};

  return executeOperation(this.s.topology, count, [this, applySkipLimit, opts, callback], {
    skipSessions: true
  });
};

var count = function(self, applySkipLimit, opts, callback) {
  if (typeof applySkipLimit === 'function') {
    callback = applySkipLimit;
    applySkipLimit = true;
  }

  if (applySkipLimit) {
    if (typeof self.cursorSkip() === 'number') opts.skip = self.cursorSkip();
    if (typeof self.cursorLimit() === 'number') opts.limit = self.cursorLimit();
  }

  // Command
  var delimiter = self.s.ns.indexOf('.');

  var command = {
    count: self.s.ns.substr(delimiter + 1),
    query: self.s.cmd.query
  };

  // Apply a readConcern if set
  if (self.s.cmd.readConcern) {
    command.readConcern = self.s.cmd.readConcern;
  }

  // Apply a hint if set
  if (self.s.cmd.hint) {
    command.hint = self.s.cmd.hint;
  }

  if (typeof opts.maxTimeMS === 'number') {
    command.maxTimeMS = opts.maxTimeMS;
  } else if (self.s.cmd && typeof self.s.cmd.maxTimeMS === 'number') {
    command.maxTimeMS = self.s.cmd.maxTimeMS;
  }

  // Merge in any options
  if (opts.skip) command.skip = opts.skip;
  if (opts.limit) command.limit = opts.limit;
  if (self.s.options.hint) command.hint = self.s.options.hint;

  // Set cursor server to the same as the topology
  self.server = self.topology.s.coreTopology;

  // Execute the command
  self.s.topology.command(
    f('%s.$cmd', self.s.ns.substr(0, delimiter)),
    command,
    function(err, result) {
      callback(err, result ? result.result.n : null);
    },
    self.options
  );
};

define.classMethod('count', { callback: true, promise: true });

/**
 * Close the cursor, sending a KillCursor command and emitting close.
 * @method
 * @param {object} [options] Optional settings.
 * @param {boolean} [options.skipKillCursors] Bypass calling killCursors when closing the cursor.
 * @param {Cursor~resultCallback} [callback] The result callback.
 * @return {Promise} returns Promise if no callback passed
 */
Cursor.prototype.close = function(options, callback) {
  if (typeof options === 'function') (callback = options), (options = {});
  options = Object.assign({}, { skipKillCursors: false }, options);

  this.s.state = Cursor.CLOSED;
  if (!options.skipKillCursors) {
    // Kill the cursor
    this.kill();
  }

  const completeClose = () => {
    // Emit the close event for the cursor
    this.emit('close');

    // Callback if provided
    if (typeof callback === 'function') {
      return handleCallback(callback, null, this);
    }

    // Return a Promise
    return new this.s.promiseLibrary(function(resolve) {
      resolve();
    });
  };

  if (this.s.session) {
    return this.s.session.endSession(() => completeClose());
  }

  return completeClose();
};

define.classMethod('close', { callback: true, promise: true });

/**
 * Map all documents using the provided function
 * @method
 * @param {function} [transform] The mapping transformation method.
 * @return {Cursor}
 */
Cursor.prototype.map = function(transform) {
  if (this.cursorState.transforms && this.cursorState.transforms.doc) {
    var oldTransform = this.cursorState.transforms.doc;
    this.cursorState.transforms.doc = function(doc) {
      return transform(oldTransform(doc));
    };
  } else {
    this.cursorState.transforms = { doc: transform };
  }
  return this;
};

define.classMethod('map', { callback: false, promise: false, returns: [Cursor] });

/**
 * Is the cursor closed
 * @method
 * @return {boolean}
 */
Cursor.prototype.isClosed = function() {
  return this.isDead();
};

define.classMethod('isClosed', { callback: false, promise: false, returns: [Boolean] });

Cursor.prototype.destroy = function(err) {
  if (err) this.emit('error', err);
  this.pause();
  this.close();
};

define.classMethod('destroy', { callback: false, promise: false });

/**
 * Return a modified Readable stream including a possible transform method.
 * @method
 * @param {object} [options=null] Optional settings.
 * @param {function} [options.transform=null] A transformation method applied to each document emitted by the stream.
 * @return {Cursor}
 */
Cursor.prototype.stream = function(options) {
  this.s.streamOptions = options || {};
  return this;
};

define.classMethod('stream', { callback: false, promise: false, returns: [Cursor] });

/**
 * Execute the explain for the cursor
 * @method
 * @param {Cursor~resultCallback} [callback] The result callback.
 * @return {Promise} returns Promise if no callback passed
 */
Cursor.prototype.explain = function(callback) {
  this.s.cmd.explain = true;

  // Do we have a readConcern
  if (this.s.cmd.readConcern) {
    delete this.s.cmd['readConcern'];
  }

  return executeOperation(this.s.topology, this._next.bind(this), [callback], {
    skipSessions: true
  });
};

define.classMethod('explain', { callback: true, promise: true });

Cursor.prototype._read = function() {
  var self = this;
  if (self.s.state === Cursor.CLOSED || self.isDead()) {
    return self.push(null);
  }

  // Get the next item
  self.next(function(err, result) {
    if (err) {
      if (self.listeners('error') && self.listeners('error').length > 0) {
        self.emit('error', err);
      }
      if (!self.isDead()) self.close();

      // Emit end event
      self.emit('end');
      return self.emit('finish');
    }

    // If we provided a transformation method
    if (typeof self.s.streamOptions.transform === 'function' && result != null) {
      return self.push(self.s.streamOptions.transform(result));
    }

    // If we provided a map function
    if (
      self.cursorState.transforms &&
      typeof self.cursorState.transforms.doc === 'function' &&
      result != null
    ) {
      return self.push(self.cursorState.transforms.doc(result));
    }

    // Return the result
    self.push(result);

    if (result === null && self.isDead()) {
      self.once('end', () => {
        self.close();
        self.emit('finish');
      });
    }
  });
};

Object.defineProperty(Cursor.prototype, 'readPreference', {
  enumerable: true,
  get: function() {
    if (!this || !this.s) {
      return null;
    }

    return this.s.options.readPreference;
  }
});

Object.defineProperty(Cursor.prototype, 'namespace', {
  enumerable: true,
  get: function() {
    if (!this || !this.s) {
      return null;
    }

    // TODO: refactor this logic into core
    var ns = this.s.ns || '';
    var firstDot = ns.indexOf('.');
    if (firstDot < 0) {
      return {
        database: this.s.ns,
        collection: ''
      };
    }
    return {
      database: ns.substr(0, firstDot),
      collection: ns.substr(firstDot + 1)
    };
  }
});

/**
 * 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.
 * @function external:Readable#read
 * @param {number} size Optional argument to specify how much data to read.
 * @return {(String | Buffer | null)}
 */

/**
 * Call this function to cause the stream to return strings of the specified encoding instead of Buffer objects.
 * @function external:Readable#setEncoding
 * @param {string} encoding The encoding to use.
 * @return {null}
 */

/**
 * This method will cause the readable stream to resume emitting data events.
 * @function external:Readable#resume
 * @return {null}
 */

/**
 * 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.
 * @function external:Readable#pause
 * @return {null}
 */

/**
 * 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.
 * @function external:Readable#pipe
 * @param {Writable} destination The destination for writing data
 * @param {object} [options] Pipe options
 * @return {null}
 */

/**
 * This method will remove the hooks set up for a previous pipe() call.
 * @function external:Readable#unpipe
 * @param {Writable} [destination] The destination for writing data
 * @return {null}
 */

/**
 * 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.
 * @function external:Readable#unshift
 * @param {(Buffer|string)} chunk Chunk of data to unshift onto the read queue.
 * @return {null}
 */

/**
 * 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.)
 * @function external:Readable#wrap
 * @param {Stream} stream An "old style" readable stream.
 * @return {null}
 */

Cursor.INIT = 0;
Cursor.OPEN = 1;
Cursor.CLOSED = 2;
Cursor.GET_MORE = 3;

module.exports = Cursor;