mongodb 3.2.5 → 3.3.0-beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/HISTORY.md +0 -10
  2. package/index.js +4 -4
  3. package/lib/admin.js +56 -56
  4. package/lib/aggregation_cursor.js +7 -3
  5. package/lib/bulk/common.js +18 -13
  6. package/lib/change_stream.js +196 -89
  7. package/lib/collection.js +217 -169
  8. package/lib/command_cursor.js +17 -7
  9. package/lib/core/auth/auth_provider.js +158 -0
  10. package/lib/core/auth/defaultAuthProviders.js +29 -0
  11. package/lib/core/auth/gssapi.js +241 -0
  12. package/lib/core/auth/mongo_credentials.js +81 -0
  13. package/lib/core/auth/mongocr.js +51 -0
  14. package/lib/core/auth/plain.js +35 -0
  15. package/lib/core/auth/scram.js +293 -0
  16. package/lib/core/auth/sspi.js +131 -0
  17. package/lib/core/auth/x509.js +26 -0
  18. package/lib/core/connection/apm.js +236 -0
  19. package/lib/core/connection/command_result.js +36 -0
  20. package/lib/core/connection/commands.js +507 -0
  21. package/lib/core/connection/connect.js +370 -0
  22. package/lib/core/connection/connection.js +624 -0
  23. package/lib/core/connection/logger.js +246 -0
  24. package/lib/core/connection/msg.js +219 -0
  25. package/lib/core/connection/pool.js +1285 -0
  26. package/lib/core/connection/utils.js +57 -0
  27. package/lib/core/cursor.js +752 -0
  28. package/lib/core/error.js +186 -0
  29. package/lib/core/index.js +50 -0
  30. package/lib/core/sdam/monitoring.js +228 -0
  31. package/lib/core/sdam/server.js +467 -0
  32. package/lib/core/sdam/server_description.js +163 -0
  33. package/lib/core/sdam/server_selectors.js +244 -0
  34. package/lib/core/sdam/srv_polling.js +135 -0
  35. package/lib/core/sdam/topology.js +1151 -0
  36. package/lib/core/sdam/topology_description.js +408 -0
  37. package/lib/core/sessions.js +711 -0
  38. package/lib/core/tools/smoke_plugin.js +61 -0
  39. package/lib/core/topologies/mongos.js +1337 -0
  40. package/lib/core/topologies/read_preference.js +202 -0
  41. package/lib/core/topologies/replset.js +1507 -0
  42. package/lib/core/topologies/replset_state.js +1121 -0
  43. package/lib/core/topologies/server.js +984 -0
  44. package/lib/core/topologies/shared.js +453 -0
  45. package/lib/core/transactions.js +167 -0
  46. package/lib/core/uri_parser.js +631 -0
  47. package/lib/core/utils.js +165 -0
  48. package/lib/core/wireprotocol/command.js +170 -0
  49. package/lib/core/wireprotocol/compression.js +73 -0
  50. package/lib/core/wireprotocol/constants.js +13 -0
  51. package/lib/core/wireprotocol/get_more.js +86 -0
  52. package/lib/core/wireprotocol/index.js +18 -0
  53. package/lib/core/wireprotocol/kill_cursors.js +70 -0
  54. package/lib/core/wireprotocol/query.js +224 -0
  55. package/lib/core/wireprotocol/shared.js +115 -0
  56. package/lib/core/wireprotocol/write_command.js +50 -0
  57. package/lib/cursor.js +40 -46
  58. package/lib/db.js +141 -95
  59. package/lib/dynamic_loaders.js +32 -0
  60. package/lib/error.js +12 -10
  61. package/lib/gridfs/chunk.js +2 -2
  62. package/lib/gridfs/grid_store.js +31 -25
  63. package/lib/gridfs-stream/index.js +4 -4
  64. package/lib/gridfs-stream/upload.js +1 -1
  65. package/lib/mongo_client.js +37 -15
  66. package/lib/operations/add_user.js +96 -0
  67. package/lib/operations/aggregate.js +24 -13
  68. package/lib/operations/aggregate_operation.js +127 -0
  69. package/lib/operations/bulk_write.js +104 -0
  70. package/lib/operations/close.js +47 -0
  71. package/lib/operations/collection_ops.js +28 -287
  72. package/lib/operations/collections.js +55 -0
  73. package/lib/operations/command.js +120 -0
  74. package/lib/operations/command_v2.js +43 -0
  75. package/lib/operations/common_functions.js +372 -0
  76. package/lib/operations/{mongo_client_ops.js → connect.js} +185 -157
  77. package/lib/operations/count.js +72 -0
  78. package/lib/operations/count_documents.js +46 -0
  79. package/lib/operations/create_collection.js +118 -0
  80. package/lib/operations/create_index.js +92 -0
  81. package/lib/operations/create_indexes.js +61 -0
  82. package/lib/operations/cursor_ops.js +3 -4
  83. package/lib/operations/db_ops.js +15 -12
  84. package/lib/operations/delete_many.js +25 -0
  85. package/lib/operations/delete_one.js +25 -0
  86. package/lib/operations/distinct.js +85 -0
  87. package/lib/operations/drop.js +53 -0
  88. package/lib/operations/drop_index.js +42 -0
  89. package/lib/operations/drop_indexes.js +23 -0
  90. package/lib/operations/estimated_document_count.js +33 -0
  91. package/lib/operations/execute_db_admin_command.js +34 -0
  92. package/lib/operations/execute_operation.js +165 -0
  93. package/lib/operations/explain.js +23 -0
  94. package/lib/operations/find_and_modify.js +98 -0
  95. package/lib/operations/find_one.js +33 -0
  96. package/lib/operations/find_one_and_delete.js +16 -0
  97. package/lib/operations/find_one_and_replace.js +18 -0
  98. package/lib/operations/find_one_and_update.js +19 -0
  99. package/lib/operations/geo_haystack_search.js +79 -0
  100. package/lib/operations/has_next.js +40 -0
  101. package/lib/operations/index_exists.js +39 -0
  102. package/lib/operations/index_information.js +23 -0
  103. package/lib/operations/indexes.js +22 -0
  104. package/lib/operations/insert_many.js +63 -0
  105. package/lib/operations/insert_one.js +75 -0
  106. package/lib/operations/is_capped.js +19 -0
  107. package/lib/operations/list_indexes.js +66 -0
  108. package/lib/operations/map_reduce.js +189 -0
  109. package/lib/operations/next.js +32 -0
  110. package/lib/operations/operation.js +63 -0
  111. package/lib/operations/options_operation.js +32 -0
  112. package/lib/operations/profiling_level.js +31 -0
  113. package/lib/operations/re_index.js +28 -0
  114. package/lib/operations/remove_user.js +52 -0
  115. package/lib/operations/rename.js +61 -0
  116. package/lib/operations/replace_one.js +47 -0
  117. package/lib/operations/set_profiling_level.js +48 -0
  118. package/lib/operations/stats.js +45 -0
  119. package/lib/operations/to_array.js +68 -0
  120. package/lib/operations/update_many.js +29 -0
  121. package/lib/operations/update_one.js +44 -0
  122. package/lib/operations/validate_collection.js +40 -0
  123. package/lib/read_concern.js +55 -0
  124. package/lib/topologies/mongos.js +3 -3
  125. package/lib/topologies/native_topology.js +22 -2
  126. package/lib/topologies/replset.js +3 -3
  127. package/lib/topologies/server.js +4 -4
  128. package/lib/topologies/topology_base.js +6 -6
  129. package/lib/url_parser.js +4 -3
  130. package/lib/utils.js +46 -59
  131. package/lib/write_concern.js +66 -0
  132. package/package.json +15 -6
  133. package/lib/.DS_Store +0 -0
@@ -0,0 +1,104 @@
1
+ 'use strict';
2
+
3
+ const applyRetryableWrites = require('../utils').applyRetryableWrites;
4
+ const applyWriteConcern = require('../utils').applyWriteConcern;
5
+ const MongoError = require('../core').MongoError;
6
+ const OperationBase = require('./operation').OperationBase;
7
+
8
+ class BulkWriteOperation extends OperationBase {
9
+ constructor(collection, operations, options) {
10
+ super(options);
11
+
12
+ this.collection = collection;
13
+ this.operations = operations;
14
+ }
15
+
16
+ execute(callback) {
17
+ const coll = this.collection;
18
+ const operations = this.operations;
19
+ let options = this.options;
20
+
21
+ // Add ignoreUndfined
22
+ if (coll.s.options.ignoreUndefined) {
23
+ options = Object.assign({}, options);
24
+ options.ignoreUndefined = coll.s.options.ignoreUndefined;
25
+ }
26
+
27
+ // Create the bulk operation
28
+ const bulk =
29
+ options.ordered === true || options.ordered == null
30
+ ? coll.initializeOrderedBulkOp(options)
31
+ : coll.initializeUnorderedBulkOp(options);
32
+
33
+ // Do we have a collation
34
+ let collation = false;
35
+
36
+ // for each op go through and add to the bulk
37
+ try {
38
+ for (let i = 0; i < operations.length; i++) {
39
+ // Get the operation type
40
+ const key = Object.keys(operations[i])[0];
41
+ // Check if we have a collation
42
+ if (operations[i][key].collation) {
43
+ collation = true;
44
+ }
45
+
46
+ // Pass to the raw bulk
47
+ bulk.raw(operations[i]);
48
+ }
49
+ } catch (err) {
50
+ return callback(err, null);
51
+ }
52
+
53
+ // Final options for retryable writes and write concern
54
+ let finalOptions = Object.assign({}, options);
55
+ finalOptions = applyRetryableWrites(finalOptions, coll.s.db);
56
+ finalOptions = applyWriteConcern(finalOptions, { db: coll.s.db, collection: coll }, options);
57
+
58
+ const writeCon = finalOptions.writeConcern ? finalOptions.writeConcern : {};
59
+ const capabilities = coll.s.topology.capabilities();
60
+
61
+ // Did the user pass in a collation, check if our write server supports it
62
+ if (collation && capabilities && !capabilities.commandsTakeCollation) {
63
+ return callback(new MongoError('server/primary/mongos does not support collation'));
64
+ }
65
+
66
+ // Execute the bulk
67
+ bulk.execute(writeCon, finalOptions, (err, r) => {
68
+ // We have connection level error
69
+ if (!r && err) {
70
+ return callback(err, null);
71
+ }
72
+
73
+ r.insertedCount = r.nInserted;
74
+ r.matchedCount = r.nMatched;
75
+ r.modifiedCount = r.nModified || 0;
76
+ r.deletedCount = r.nRemoved;
77
+ r.upsertedCount = r.getUpsertedIds().length;
78
+ r.upsertedIds = {};
79
+ r.insertedIds = {};
80
+
81
+ // Update the n
82
+ r.n = r.insertedCount;
83
+
84
+ // Inserted documents
85
+ const inserted = r.getInsertedIds();
86
+ // Map inserted ids
87
+ for (let i = 0; i < inserted.length; i++) {
88
+ r.insertedIds[inserted[i].index] = inserted[i]._id;
89
+ }
90
+
91
+ // Upserted documents
92
+ const upserted = r.getUpsertedIds();
93
+ // Map upserted ids
94
+ for (let i = 0; i < upserted.length; i++) {
95
+ r.upsertedIds[upserted[i].index] = upserted[i]._id;
96
+ }
97
+
98
+ // Return the results
99
+ callback(null, r);
100
+ });
101
+ }
102
+ }
103
+
104
+ module.exports = BulkWriteOperation;
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ const Aspect = require('./operation').Aspect;
4
+ const defineAspects = require('./operation').defineAspects;
5
+ const OperationBase = require('./operation').OperationBase;
6
+
7
+ class CloseOperation extends OperationBase {
8
+ constructor(client, force) {
9
+ super();
10
+ this.client = client;
11
+ this.force = force;
12
+ }
13
+
14
+ execute(callback) {
15
+ const client = this.client;
16
+ const force = this.force;
17
+ const completeClose = err => {
18
+ client.emit('close', client);
19
+ for (const name in client.s.dbCache) {
20
+ client.s.dbCache[name].emit('close', client);
21
+ }
22
+
23
+ client.removeAllListeners('close');
24
+ callback(err, null);
25
+ };
26
+ const mongocryptdClientClose = err => {
27
+ const mongocryptdClient = client.s.mongocryptdClient;
28
+ if (!mongocryptdClient) {
29
+ completeClose(err);
30
+ return;
31
+ }
32
+
33
+ mongocryptdClient.close(force, err2 => completeClose(err || err2));
34
+ };
35
+
36
+ if (client.topology == null) {
37
+ mongocryptdClientClose();
38
+ return;
39
+ }
40
+
41
+ client.topology.close(force, mongocryptdClientClose);
42
+ }
43
+ }
44
+
45
+ defineAspects(CloseOperation, [Aspect.SKIP_SESSION]);
46
+
47
+ module.exports = CloseOperation;
@@ -3,9 +3,8 @@
3
3
  const applyWriteConcern = require('../utils').applyWriteConcern;
4
4
  const applyRetryableWrites = require('../utils').applyRetryableWrites;
5
5
  const checkCollectionName = require('../utils').checkCollectionName;
6
- const Code = require('mongodb-core').BSON.Code;
6
+ const Code = require('../core').BSON.Code;
7
7
  const createIndexDb = require('./db_ops').createIndex;
8
- const decorateCommand = require('../utils').decorateCommand;
9
8
  const decorateWithCollation = require('../utils').decorateWithCollation;
10
9
  const decorateWithReadConcern = require('../utils').decorateWithReadConcern;
11
10
  const ensureIndexDb = require('./db_ops').ensureIndex;
@@ -16,10 +15,9 @@ const formattedOrderClause = require('../utils').formattedOrderClause;
16
15
  const resolveReadPreference = require('../utils').resolveReadPreference;
17
16
  const handleCallback = require('../utils').handleCallback;
18
17
  const indexInformationDb = require('./db_ops').indexInformation;
19
- const isObject = require('../utils').isObject;
20
- const Long = require('mongodb-core').BSON.Long;
21
- const MongoError = require('mongodb-core').MongoError;
22
- const ReadPreference = require('mongodb-core').ReadPreference;
18
+ const Long = require('../core').BSON.Long;
19
+ const MongoError = require('../core').MongoError;
20
+ const ReadPreference = require('../core').ReadPreference;
23
21
  const toError = require('../utils').toError;
24
22
 
25
23
  let collection;
@@ -29,13 +27,6 @@ function loadCollection() {
29
27
  }
30
28
  return collection;
31
29
  }
32
- let db;
33
- function loadDb() {
34
- if (!db) {
35
- db = require('../db');
36
- }
37
- return db;
38
- }
39
30
 
40
31
  /**
41
32
  * Group function helper
@@ -165,6 +156,10 @@ function bulkWrite(coll, operations, options, callback) {
165
156
 
166
157
  // Check the update operation to ensure it has atomic operators.
167
158
  function checkForAtomicOperators(update) {
159
+ if (Array.isArray(update)) {
160
+ return update.reduce((err, u) => err || checkForAtomicOperators(u), null);
161
+ }
162
+
168
163
  const keys = Object.keys(update);
169
164
 
170
165
  // same errors as the server would give for update doc lacking atomic operators
@@ -189,12 +184,9 @@ function checkForAtomicOperators(update) {
189
184
  function count(coll, query, options, callback) {
190
185
  if (typeof options === 'function') (callback = options), (options = {});
191
186
  options = Object.assign({}, options);
192
- options.collectionName = coll.s.name;
187
+ options.collectionName = coll.collectionName;
193
188
 
194
- options.readPreference = resolveReadPreference(options, {
195
- db: coll.s.db,
196
- collection: coll
197
- });
189
+ options.readPreference = resolveReadPreference(coll, options);
198
190
 
199
191
  let cmd;
200
192
  try {
@@ -291,7 +283,7 @@ function buildCountCommand(collectionOrCursor, query, options) {
291
283
  * @param {Collection~resultCallback} [callback] The command result callback
292
284
  */
293
285
  function createIndex(coll, fieldOrSpec, options, callback) {
294
- createIndexDb(coll.s.db, coll.s.name, fieldOrSpec, options, callback);
286
+ createIndexDb(coll.s.db, coll.collectionName, fieldOrSpec, options, callback);
295
287
  }
296
288
 
297
289
  /**
@@ -333,7 +325,7 @@ function createIndexes(coll, indexSpecs, options, callback) {
333
325
  executeCommand(
334
326
  coll.s.db,
335
327
  {
336
- createIndexes: coll.s.name,
328
+ createIndexes: coll.collectionName,
337
329
  indexes: indexSpecs
338
330
  },
339
331
  options,
@@ -378,51 +370,6 @@ function deleteOne(coll, filter, options, callback) {
378
370
  removeDocuments(coll, filter, options, (err, r) => deleteCallback(err, r, callback));
379
371
  }
380
372
 
381
- /**
382
- * Return a list of distinct values for the given key across a collection.
383
- *
384
- * @method
385
- * @param {Collection} a Collection instance.
386
- * @param {string} key Field of the document to find distinct values for.
387
- * @param {object} query The query for filtering the set of documents to which we apply the distinct filter.
388
- * @param {object} [options] Optional settings. See Collection.prototype.distinct for a list of options.
389
- * @param {Collection~resultCallback} [callback] The command result callback
390
- */
391
- function distinct(coll, key, query, options, callback) {
392
- // maxTimeMS option
393
- const maxTimeMS = options.maxTimeMS;
394
-
395
- // Distinct command
396
- const cmd = {
397
- distinct: coll.s.name,
398
- key: key,
399
- query: query
400
- };
401
-
402
- options = Object.assign({}, options);
403
- // Ensure we have the right read preference inheritance
404
- options.readPreference = resolveReadPreference(options, { db: coll.s.db, collection: coll });
405
-
406
- // Add maxTimeMS if defined
407
- if (typeof maxTimeMS === 'number') cmd.maxTimeMS = maxTimeMS;
408
-
409
- // Do we have a readConcern specified
410
- decorateWithReadConcern(cmd, coll, options);
411
-
412
- // Have we specified collation
413
- try {
414
- decorateWithCollation(cmd, coll, options);
415
- } catch (err) {
416
- return callback(err, null);
417
- }
418
-
419
- // Execute the command
420
- executeCommand(coll.s.db, cmd, options, (err, result) => {
421
- if (err) return handleCallback(callback, err);
422
- handleCallback(callback, null, result.values);
423
- });
424
- }
425
-
426
373
  /**
427
374
  * Drop an index from this collection.
428
375
  *
@@ -434,7 +381,7 @@ function distinct(coll, key, query, options, callback) {
434
381
  */
435
382
  function dropIndex(coll, indexName, options, callback) {
436
383
  // Delete index command
437
- const cmd = { dropIndexes: coll.s.name, index: indexName };
384
+ const cmd = { dropIndexes: coll.collectionName, index: indexName };
438
385
 
439
386
  // Decorate command with writeConcern if supported
440
387
  applyWriteConcern(cmd, { db: coll.s.db, collection: coll }, options);
@@ -472,7 +419,7 @@ function dropIndexes(coll, options, callback) {
472
419
  * @param {Collection~resultCallback} [callback] The command result callback
473
420
  */
474
421
  function ensureIndex(coll, fieldOrSpec, options, callback) {
475
- ensureIndexDb(coll.s.db, coll.s.name, fieldOrSpec, options, callback);
422
+ ensureIndexDb(coll.s.db, coll.collectionName, fieldOrSpec, options, callback);
476
423
  }
477
424
 
478
425
  /**
@@ -490,7 +437,7 @@ function ensureIndex(coll, fieldOrSpec, options, callback) {
490
437
  function findAndModify(coll, query, sort, doc, options, callback) {
491
438
  // Create findAndModify command object
492
439
  const queryObject = {
493
- findAndModify: coll.s.name,
440
+ findAndModify: coll.collectionName,
494
441
  query: query
495
442
  };
496
443
 
@@ -661,43 +608,6 @@ function findOneAndUpdate(coll, filter, update, options, callback) {
661
608
  findAndModify(coll, filter, options.sort, update, finalOptions, callback);
662
609
  }
663
610
 
664
- /**
665
- * Execute a geo search using a geo haystack index on a collection.
666
- *
667
- * @method
668
- * @param {Collection} a Collection instance.
669
- * @param {number} x Point to search on the x axis, ensure the indexes are ordered in the same order.
670
- * @param {number} y Point to search on the y axis, ensure the indexes are ordered in the same order.
671
- * @param {object} [options] Optional settings. See Collection.prototype.geoHaystackSearch for a list of options.
672
- * @param {Collection~resultCallback} [callback] The command result callback
673
- */
674
- function geoHaystackSearch(coll, x, y, options, callback) {
675
- // Build command object
676
- let commandObject = {
677
- geoSearch: coll.s.name,
678
- near: [x, y]
679
- };
680
-
681
- // Remove read preference from hash if it exists
682
- commandObject = decorateCommand(commandObject, options, ['readPreference', 'session']);
683
-
684
- options = Object.assign({}, options);
685
- // Ensure we have the right read preference inheritance
686
- options.readPreference = resolveReadPreference(options, { db: coll.s.db, collection: coll });
687
-
688
- // Do we have a readConcern specified
689
- decorateWithReadConcern(commandObject, coll, options);
690
-
691
- // Execute the command
692
- executeCommand(coll.s.db, commandObject, options, (err, res) => {
693
- if (err) return handleCallback(callback, err);
694
- if (res.err || res.errmsg) handleCallback(callback, toError(res));
695
- // should we only be returning res.results here? Not sure if the user
696
- // should see the other return information
697
- handleCallback(callback, null, res);
698
- });
699
- }
700
-
701
611
  /**
702
612
  * Run a group command across a collection.
703
613
  *
@@ -720,7 +630,7 @@ function group(coll, keys, condition, initial, reduce, finalize, command, option
720
630
 
721
631
  const selector = {
722
632
  group: {
723
- ns: coll.s.name,
633
+ ns: coll.collectionName,
724
634
  $reduce: reduceFunction,
725
635
  cond: condition,
726
636
  initial: initial,
@@ -743,7 +653,7 @@ function group(coll, keys, condition, initial, reduce, finalize, command, option
743
653
 
744
654
  options = Object.assign({}, options);
745
655
  // Ensure we have the right read preference inheritance
746
- options.readPreference = resolveReadPreference(options, { db: coll.s.db, collection: coll });
656
+ options.readPreference = resolveReadPreference(coll, options);
747
657
 
748
658
  // Do we have a readConcern specified
749
659
  decorateWithReadConcern(selector, coll, options);
@@ -764,7 +674,7 @@ function group(coll, keys, condition, initial, reduce, finalize, command, option
764
674
  // Create execution scope
765
675
  const scope = reduce != null && reduce._bsontype === 'Code' ? reduce.scope : {};
766
676
 
767
- scope.ns = coll.s.name;
677
+ scope.ns = coll.collectionName;
768
678
  scope.keys = keys;
769
679
  scope.condition = condition;
770
680
  scope.initial = initial;
@@ -789,7 +699,7 @@ function group(coll, keys, condition, initial, reduce, finalize, command, option
789
699
  */
790
700
  function indexes(coll, options, callback) {
791
701
  options = Object.assign({}, { full: true }, options);
792
- indexInformationDb(coll.s.db, coll.s.name, options, callback);
702
+ indexInformationDb(coll.s.db, coll.collectionName, options, callback);
793
703
  }
794
704
 
795
705
  /**
@@ -829,7 +739,7 @@ function indexExists(coll, indexes, options, callback) {
829
739
  * @param {Collection~resultCallback} [callback] The command result callback
830
740
  */
831
741
  function indexInformation(coll, options, callback) {
832
- indexInformationDb(coll.s.db, coll.s.name, options, callback);
742
+ indexInformationDb(coll.s.db, coll.collectionName, options, callback);
833
743
  }
834
744
 
835
745
  function insertDocuments(coll, docs, options, callback) {
@@ -958,120 +868,6 @@ function isCapped(coll, options, callback) {
958
868
  });
959
869
  }
960
870
 
961
- /**
962
- * Run Map Reduce across a collection. Be aware that the inline option for out will return an array of results not a collection.
963
- *
964
- * @method
965
- * @param {Collection} a Collection instance.
966
- * @param {(function|string)} map The mapping function.
967
- * @param {(function|string)} reduce The reduce function.
968
- * @param {object} [options] Optional settings. See Collection.prototype.mapReduce for a list of options.
969
- * @param {Collection~resultCallback} [callback] The command result callback
970
- */
971
- function mapReduce(coll, map, reduce, options, callback) {
972
- const mapCommandHash = {
973
- mapreduce: coll.s.name,
974
- map: map,
975
- reduce: reduce
976
- };
977
-
978
- // Exclusion list
979
- const exclusionList = ['readPreference', 'session', 'bypassDocumentValidation'];
980
-
981
- // Add any other options passed in
982
- for (let n in options) {
983
- if ('scope' === n) {
984
- mapCommandHash[n] = processScope(options[n]);
985
- } else {
986
- // Only include if not in exclusion list
987
- if (exclusionList.indexOf(n) === -1) {
988
- mapCommandHash[n] = options[n];
989
- }
990
- }
991
- }
992
-
993
- options = Object.assign({}, options);
994
-
995
- // Ensure we have the right read preference inheritance
996
- options.readPreference = resolveReadPreference(options, { db: coll.s.db, collection: coll });
997
-
998
- // If we have a read preference and inline is not set as output fail hard
999
- if (
1000
- options.readPreference !== false &&
1001
- options.readPreference !== 'primary' &&
1002
- options['out'] &&
1003
- (options['out'].inline !== 1 && options['out'] !== 'inline')
1004
- ) {
1005
- // Force readPreference to primary
1006
- options.readPreference = 'primary';
1007
- // Decorate command with writeConcern if supported
1008
- applyWriteConcern(mapCommandHash, { db: coll.s.db, collection: coll }, options);
1009
- } else {
1010
- decorateWithReadConcern(mapCommandHash, coll, options);
1011
- }
1012
-
1013
- // Is bypassDocumentValidation specified
1014
- if (options.bypassDocumentValidation === true) {
1015
- mapCommandHash.bypassDocumentValidation = options.bypassDocumentValidation;
1016
- }
1017
-
1018
- // Have we specified collation
1019
- try {
1020
- decorateWithCollation(mapCommandHash, coll, options);
1021
- } catch (err) {
1022
- return callback(err, null);
1023
- }
1024
-
1025
- // Execute command
1026
- executeCommand(coll.s.db, mapCommandHash, options, (err, result) => {
1027
- if (err) return handleCallback(callback, err);
1028
- // Check if we have an error
1029
- if (1 !== result.ok || result.err || result.errmsg) {
1030
- return handleCallback(callback, toError(result));
1031
- }
1032
-
1033
- // Create statistics value
1034
- const stats = {};
1035
- if (result.timeMillis) stats['processtime'] = result.timeMillis;
1036
- if (result.counts) stats['counts'] = result.counts;
1037
- if (result.timing) stats['timing'] = result.timing;
1038
-
1039
- // invoked with inline?
1040
- if (result.results) {
1041
- // If we wish for no verbosity
1042
- if (options['verbose'] == null || !options['verbose']) {
1043
- return handleCallback(callback, null, result.results);
1044
- }
1045
-
1046
- return handleCallback(callback, null, { results: result.results, stats: stats });
1047
- }
1048
-
1049
- // The returned collection
1050
- let collection = null;
1051
-
1052
- // If we have an object it's a different db
1053
- if (result.result != null && typeof result.result === 'object') {
1054
- const doc = result.result;
1055
- // Return a collection from another db
1056
- let Db = loadDb();
1057
- collection = new Db(doc.db, coll.s.db.s.topology, coll.s.db.s.options).collection(
1058
- doc.collection
1059
- );
1060
- } else {
1061
- // Create a collection object that wraps the result collection
1062
- collection = coll.s.db.collection(result.result);
1063
- }
1064
-
1065
- // If we wish for no verbosity
1066
- if (options['verbose'] == null || !options['verbose']) {
1067
- return handleCallback(callback, err, collection);
1068
- }
1069
-
1070
- // Return stats as third set of values
1071
- handleCallback(callback, err, { collection: collection, stats: stats });
1072
- });
1073
- }
1074
-
1075
871
  /**
1076
872
  * Return the options of the collection.
1077
873
  *
@@ -1081,12 +877,12 @@ function mapReduce(coll, map, reduce, options, callback) {
1081
877
  * @param {Collection~resultCallback} [callback] The results callback
1082
878
  */
1083
879
  function optionsOp(coll, opts, callback) {
1084
- coll.s.db.listCollections({ name: coll.s.name }, opts).toArray((err, collections) => {
880
+ coll.s.db.listCollections({ name: coll.collectionName }, opts).toArray((err, collections) => {
1085
881
  if (err) return handleCallback(callback, err);
1086
882
  if (collections.length === 0) {
1087
883
  return handleCallback(
1088
884
  callback,
1089
- MongoError.create({ message: `collection ${coll.s.namespace} not found`, driver: true })
885
+ MongoError.create({ message: `collection ${coll.namespace} not found`, driver: true })
1090
886
  );
1091
887
  }
1092
888
 
@@ -1106,7 +902,7 @@ function optionsOp(coll, opts, callback) {
1106
902
  function parallelCollectionScan(coll, options, callback) {
1107
903
  // Create command object
1108
904
  const commandObject = {
1109
- parallelCollectionScan: coll.s.name,
905
+ parallelCollectionScan: coll.collectionName,
1110
906
  numCursors: options.numCursors
1111
907
  };
1112
908
 
@@ -1138,7 +934,7 @@ function parallelCollectionScan(coll, options, callback) {
1138
934
  // Convert cursorId to Long if needed
1139
935
  const cursorId = typeof rawId === 'number' ? Long.fromNumber(rawId) : rawId;
1140
936
  // Add a command cursor
1141
- cursors.push(coll.s.topology.cursor(coll.s.namespace, cursorId, options));
937
+ cursors.push(coll.s.topology.cursor(coll.namespace, cursorId, options));
1142
938
  }
1143
939
 
1144
940
  handleCallback(callback, null, cursors);
@@ -1166,32 +962,6 @@ function prepareDocs(coll, docs, options) {
1166
962
  });
1167
963
  }
1168
964
 
1169
- /**
1170
- * Functions that are passed as scope args must
1171
- * be converted to Code instances.
1172
- * @ignore
1173
- */
1174
- function processScope(scope) {
1175
- if (!isObject(scope) || scope._bsontype === 'ObjectID') {
1176
- return scope;
1177
- }
1178
-
1179
- const keys = Object.keys(scope);
1180
- let key;
1181
- const new_scope = {};
1182
-
1183
- for (let i = keys.length - 1; i >= 0; i--) {
1184
- key = keys[i];
1185
- if ('function' === typeof scope[key]) {
1186
- new_scope[key] = new Code(String(scope[key]));
1187
- } else {
1188
- new_scope[key] = processScope(scope[key]);
1189
- }
1190
- }
1191
-
1192
- return new_scope;
1193
- }
1194
-
1195
965
  /**
1196
966
  * Reindex all indexes on the collection.
1197
967
  *
@@ -1202,7 +972,7 @@ function processScope(scope) {
1202
972
  */
1203
973
  function reIndex(coll, options, callback) {
1204
974
  // Reindex
1205
- const cmd = { reIndex: coll.s.name };
975
+ const cmd = { reIndex: coll.collectionName };
1206
976
 
1207
977
  // Execute the command
1208
978
  executeCommand(coll.s.db, cmd, options, (err, result) => {
@@ -1274,8 +1044,8 @@ function rename(coll, newName, options, callback) {
1274
1044
  // Check the collection name
1275
1045
  checkCollectionName(newName);
1276
1046
  // Build the command
1277
- const renameCollection = `${coll.s.dbName}.${coll.s.name}`;
1278
- const toCollection = `${coll.s.dbName}.${newName}`;
1047
+ const renameCollection = coll.s.namespace.toString();
1048
+ const toCollection = coll.s.namespace.withCollection(newName);
1279
1049
  const dropTarget = typeof options.dropTarget === 'boolean' ? options.dropTarget : false;
1280
1050
  const cmd = { renameCollection: renameCollection, to: toCollection, dropTarget: dropTarget };
1281
1051
 
@@ -1294,7 +1064,7 @@ function rename(coll, newName, options, callback) {
1294
1064
  new Collection(
1295
1065
  coll.s.db,
1296
1066
  coll.s.topology,
1297
- coll.s.dbName,
1067
+ coll.databaseName,
1298
1068
  newName,
1299
1069
  coll.s.pkFactory,
1300
1070
  coll.s.options
@@ -1372,31 +1142,6 @@ function save(coll, doc, options, callback) {
1372
1142
  });
1373
1143
  }
1374
1144
 
1375
- /**
1376
- * Get all the collection statistics.
1377
- *
1378
- * @method
1379
- * @param {Collection} a Collection instance.
1380
- * @param {object} [options] Optional settings. See Collection.prototype.stats for a list of options.
1381
- * @param {Collection~resultCallback} [callback] The collection result callback
1382
- */
1383
- function stats(coll, options, callback) {
1384
- // Build command object
1385
- const commandObject = {
1386
- collStats: coll.s.name
1387
- };
1388
-
1389
- // Check if we have the scale value
1390
- if (options['scale'] != null) commandObject['scale'] = options['scale'];
1391
-
1392
- options = Object.assign({}, options);
1393
- // Ensure we have the right read preference inheritance
1394
- options.readPreference = resolveReadPreference(options, { db: coll.s.db, collection: coll });
1395
-
1396
- // Execute the command
1397
- executeCommand(coll.s.db, commandObject, options, callback);
1398
- }
1399
-
1400
1145
  function updateCallback(err, r, callback) {
1401
1146
  if (callback == null) return;
1402
1147
  if (err) return callback(err);
@@ -1512,7 +1257,6 @@ module.exports = {
1512
1257
  createIndexes,
1513
1258
  deleteMany,
1514
1259
  deleteOne,
1515
- distinct,
1516
1260
  dropIndex,
1517
1261
  dropIndexes,
1518
1262
  ensureIndex,
@@ -1522,7 +1266,6 @@ module.exports = {
1522
1266
  findOneAndDelete,
1523
1267
  findOneAndReplace,
1524
1268
  findOneAndUpdate,
1525
- geoHaystackSearch,
1526
1269
  group,
1527
1270
  indexes,
1528
1271
  indexExists,
@@ -1530,7 +1273,6 @@ module.exports = {
1530
1273
  insertMany,
1531
1274
  insertOne,
1532
1275
  isCapped,
1533
- mapReduce,
1534
1276
  optionsOp,
1535
1277
  parallelCollectionScan,
1536
1278
  prepareDocs,
@@ -1539,7 +1281,6 @@ module.exports = {
1539
1281
  rename,
1540
1282
  replaceOne,
1541
1283
  save,
1542
- stats,
1543
1284
  updateDocuments,
1544
1285
  updateMany,
1545
1286
  updateOne