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,92 @@
1
+ 'use strict';
2
+
3
+ const Aspect = require('./operation').Aspect;
4
+ const CommandOperation = require('./command');
5
+ const defineAspects = require('./operation').defineAspects;
6
+ const handleCallback = require('../utils').handleCallback;
7
+ const MongoError = require('../core').MongoError;
8
+ const parseIndexOptions = require('../utils').parseIndexOptions;
9
+
10
+ const keysToOmit = new Set([
11
+ 'name',
12
+ 'key',
13
+ 'writeConcern',
14
+ 'w',
15
+ 'wtimeout',
16
+ 'j',
17
+ 'fsync',
18
+ 'readPreference',
19
+ 'session'
20
+ ]);
21
+
22
+ class CreateIndexOperation extends CommandOperation {
23
+ constructor(db, name, fieldOrSpec, options) {
24
+ super(db, options);
25
+
26
+ // Build the index
27
+ const indexParameters = parseIndexOptions(fieldOrSpec);
28
+ // Generate the index name
29
+ const indexName = typeof options.name === 'string' ? options.name : indexParameters.name;
30
+ // Set up the index
31
+ const indexesObject = { name: indexName, key: indexParameters.fieldHash };
32
+
33
+ this.name = name;
34
+ this.fieldOrSpec = fieldOrSpec;
35
+ this.indexes = indexesObject;
36
+ }
37
+
38
+ _buildCommand() {
39
+ const options = this.options;
40
+ const name = this.name;
41
+ const indexes = this.indexes;
42
+
43
+ // merge all the options
44
+ for (let optionName in options) {
45
+ if (!keysToOmit.has(optionName)) {
46
+ indexes[optionName] = options[optionName];
47
+ }
48
+ }
49
+
50
+ // Create command, apply write concern to command
51
+ const cmd = { createIndexes: name, indexes: [indexes] };
52
+
53
+ return cmd;
54
+ }
55
+
56
+ execute(callback) {
57
+ const db = this.db;
58
+ const options = this.options;
59
+ const indexes = this.indexes;
60
+
61
+ // Get capabilities
62
+ const capabilities = db.s.topology.capabilities();
63
+
64
+ // Did the user pass in a collation, check if our write server supports it
65
+ if (options.collation && capabilities && !capabilities.commandsTakeCollation) {
66
+ // Create a new error
67
+ const error = new MongoError('server/primary/mongos does not support collation');
68
+ error.code = 67;
69
+ // Return the error
70
+ return callback(error);
71
+ }
72
+
73
+ // Ensure we have a callback
74
+ if (options.writeConcern && typeof callback !== 'function') {
75
+ throw MongoError.create({
76
+ message: 'Cannot use a writeConcern without a provided callback',
77
+ driver: true
78
+ });
79
+ }
80
+
81
+ // Attempt to run using createIndexes command
82
+ super.execute((err, result) => {
83
+ if (err == null) return handleCallback(callback, err, indexes.name);
84
+
85
+ return handleCallback(callback, err, result);
86
+ });
87
+ }
88
+ }
89
+
90
+ defineAspects(CreateIndexOperation, Aspect.WRITE_OPERATION);
91
+
92
+ module.exports = CreateIndexOperation;
@@ -0,0 +1,61 @@
1
+ 'use strict';
2
+
3
+ const Aspect = require('./operation').Aspect;
4
+ const defineAspects = require('./operation').defineAspects;
5
+ const OperationBase = require('./operation').OperationBase;
6
+ const executeCommand = require('./db_ops').executeCommand;
7
+ const MongoError = require('../core').MongoError;
8
+ const ReadPreference = require('../core').ReadPreference;
9
+
10
+ class CreateIndexesOperation extends OperationBase {
11
+ constructor(collection, indexSpecs, options) {
12
+ super(options);
13
+
14
+ this.collection = collection;
15
+ this.indexSpecs = indexSpecs;
16
+ }
17
+
18
+ execute(callback) {
19
+ const coll = this.collection;
20
+ const indexSpecs = this.indexSpecs;
21
+ let options = this.options;
22
+
23
+ const capabilities = coll.s.topology.capabilities();
24
+
25
+ // Ensure we generate the correct name if the parameter is not set
26
+ for (let i = 0; i < indexSpecs.length; i++) {
27
+ if (indexSpecs[i].name == null) {
28
+ const keys = [];
29
+
30
+ // Did the user pass in a collation, check if our write server supports it
31
+ if (indexSpecs[i].collation && capabilities && !capabilities.commandsTakeCollation) {
32
+ return callback(new MongoError('server/primary/mongos does not support collation'));
33
+ }
34
+
35
+ for (let name in indexSpecs[i].key) {
36
+ keys.push(`${name}_${indexSpecs[i].key[name]}`);
37
+ }
38
+
39
+ // Set the name
40
+ indexSpecs[i].name = keys.join('_');
41
+ }
42
+ }
43
+
44
+ options = Object.assign({}, options, { readPreference: ReadPreference.PRIMARY });
45
+
46
+ // Execute the index
47
+ executeCommand(
48
+ coll.s.db,
49
+ {
50
+ createIndexes: coll.collectionName,
51
+ indexes: indexSpecs
52
+ },
53
+ options,
54
+ callback
55
+ );
56
+ }
57
+ }
58
+
59
+ defineAspects(CreateIndexesOperation, Aspect.WRITE_OPERATION);
60
+
61
+ module.exports = CreateIndexesOperation;
@@ -3,7 +3,7 @@
3
3
  const buildCountCommand = require('./collection_ops').buildCountCommand;
4
4
  const formattedOrderClause = require('../utils').formattedOrderClause;
5
5
  const handleCallback = require('../utils').handleCallback;
6
- const MongoError = require('mongodb-core').MongoError;
6
+ const MongoError = require('../core').MongoError;
7
7
  const push = Array.prototype.push;
8
8
 
9
9
  let cursor;
@@ -49,8 +49,7 @@ function count(cursor, applySkipLimit, opts, callback) {
49
49
  options.maxTimeMS = opts.maxTimeMS;
50
50
 
51
51
  // Command
52
- const delimiter = cursor.s.ns.indexOf('.');
53
- options.collectionName = cursor.s.ns.substr(delimiter + 1);
52
+ options.collectionName = cursor.s.namespace.collection;
54
53
 
55
54
  let command;
56
55
  try {
@@ -64,7 +63,7 @@ function count(cursor, applySkipLimit, opts, callback) {
64
63
 
65
64
  // Execute the command
66
65
  cursor.s.topology.command(
67
- `${cursor.s.ns.substr(0, delimiter)}.$cmd`,
66
+ cursor.s.namespace.withCollection('$cmd'),
68
67
  command,
69
68
  cursor.s.options,
70
69
  (err, result) => {
@@ -1,16 +1,17 @@
1
1
  'use strict';
2
2
 
3
3
  const applyWriteConcern = require('../utils').applyWriteConcern;
4
- const Code = require('mongodb-core').BSON.Code;
4
+ const Code = require('../core').BSON.Code;
5
5
  const resolveReadPreference = require('../utils').resolveReadPreference;
6
6
  const crypto = require('crypto');
7
7
  const debugOptions = require('../utils').debugOptions;
8
8
  const handleCallback = require('../utils').handleCallback;
9
- const MongoError = require('mongodb-core').MongoError;
9
+ const MongoError = require('../core').MongoError;
10
10
  const parseIndexOptions = require('../utils').parseIndexOptions;
11
- const ReadPreference = require('mongodb-core').ReadPreference;
11
+ const ReadPreference = require('../core').ReadPreference;
12
12
  const toError = require('../utils').toError;
13
13
  const CONSTANTS = require('../constants');
14
+ const MongoDBNamespace = require('../utils').MongoDBNamespace;
14
15
 
15
16
  const count = require('./collection_ops').count;
16
17
  const findOne = require('./collection_ops').findOne;
@@ -166,7 +167,7 @@ function collections(db, options, callback) {
166
167
  return new Collection(
167
168
  db,
168
169
  db.s.topology,
169
- db.s.databaseName,
170
+ db.databaseName,
170
171
  d.name,
171
172
  db.s.pkFactory,
172
173
  db.s.options
@@ -219,7 +220,7 @@ function createCollection(db, name, options, callback) {
219
220
  return handleCallback(
220
221
  callback,
221
222
  null,
222
- new Collection(db, db.s.topology, db.s.databaseName, name, db.s.pkFactory, options)
223
+ new Collection(db, db.s.topology, db.databaseName, name, db.s.pkFactory, options)
223
224
  );
224
225
  } catch (err) {
225
226
  return handleCallback(callback, err);
@@ -253,7 +254,7 @@ function createCollection(db, name, options, callback) {
253
254
  return handleCallback(
254
255
  callback,
255
256
  null,
256
- new Collection(db, db.s.topology, db.s.databaseName, name, db.s.pkFactory, options)
257
+ new Collection(db, db.s.topology, db.databaseName, name, db.s.pkFactory, options)
257
258
  );
258
259
  } catch (err) {
259
260
  return handleCallback(callback, err);
@@ -318,7 +319,7 @@ function createIndex(db, name, fieldOrSpec, options, callback) {
318
319
  finalOptions.checkKeys = false;
319
320
  // Insert document
320
321
  db.s.topology.insert(
321
- `${db.s.databaseName}.${CONSTANTS.SYSTEM_INDEX_COLLECTION}`,
322
+ db.s.namespace.withCollection(CONSTANTS.SYSTEM_INDEX_COLLECTION),
322
323
  doc,
323
324
  finalOptions,
324
325
  (err, result) => {
@@ -494,10 +495,10 @@ function executeCommand(db, command, options, callback) {
494
495
  if (db.serverConfig && db.serverConfig.isDestroyed())
495
496
  return callback(new MongoError('topology was destroyed'));
496
497
  // Get the db name we are executing against
497
- const dbName = options.dbName || options.authdb || db.s.databaseName;
498
+ const dbName = options.dbName || options.authdb || db.databaseName;
498
499
 
499
500
  // Convert the readPreference if its not a write
500
- options.readPreference = resolveReadPreference(options, { db, default: ReadPreference.primary });
501
+ options.readPreference = resolveReadPreference(db, options);
501
502
 
502
503
  // Debug information
503
504
  if (db.s.logger.isDebug())
@@ -510,7 +511,7 @@ function executeCommand(db, command, options, callback) {
510
511
  );
511
512
 
512
513
  // Execute command
513
- db.s.topology.command(`${dbName}.$cmd`, command, options, (err, result) => {
514
+ db.s.topology.command(db.s.namespace.withCollection('$cmd'), command, options, (err, result) => {
514
515
  if (err) return handleCallback(callback, err);
515
516
  if (options.full) return handleCallback(callback, null, result);
516
517
  handleCallback(callback, null, result.result);
@@ -527,7 +528,9 @@ function executeCommand(db, command, options, callback) {
527
528
  * @param {Db~resultCallback} [callback] The command result callback
528
529
  */
529
530
  function executeDbAdminCommand(db, command, options, callback) {
530
- db.s.topology.command('admin.$cmd', command, options, (err, result) => {
531
+ const namespace = new MongoDBNamespace('admin', '$cmd');
532
+
533
+ db.s.topology.command(namespace, command, options, (err, result) => {
531
534
  // Did the user destroy the topology
532
535
  if (db.serverConfig && db.serverConfig.isDestroyed()) {
533
536
  return callback(new MongoError('topology was destroyed'));
@@ -748,7 +751,7 @@ function createCreateIndexCommand(db, name, fieldOrSpec, options) {
748
751
  // Generate the index name
749
752
  const indexName = typeof options.name === 'string' ? options.name : indexParameters.name;
750
753
  const selector = {
751
- ns: db.databaseName + '.' + name,
754
+ ns: db.s.namespace.withCollection(name).toString(),
752
755
  key: fieldHash,
753
756
  name: indexName
754
757
  };
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ const OperationBase = require('./operation').OperationBase;
4
+ const deleteCallback = require('./common_functions').deleteCallback;
5
+ const removeDocuments = require('./common_functions').removeDocuments;
6
+
7
+ class DeleteManyOperation extends OperationBase {
8
+ constructor(collection, filter, options) {
9
+ super(options);
10
+
11
+ this.collection = collection;
12
+ this.filter = filter;
13
+ }
14
+
15
+ execute(callback) {
16
+ const coll = this.collection;
17
+ const filter = this.filter;
18
+ const options = this.options;
19
+
20
+ options.single = false;
21
+ removeDocuments(coll, filter, options, (err, r) => deleteCallback(err, r, callback));
22
+ }
23
+ }
24
+
25
+ module.exports = DeleteManyOperation;
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ const OperationBase = require('./operation').OperationBase;
4
+ const deleteCallback = require('./common_functions').deleteCallback;
5
+ const removeDocuments = require('./common_functions').removeDocuments;
6
+
7
+ class DeleteOneOperation extends OperationBase {
8
+ constructor(collection, filter, options) {
9
+ super(options);
10
+
11
+ this.collection = collection;
12
+ this.filter = filter;
13
+ }
14
+
15
+ execute(callback) {
16
+ const coll = this.collection;
17
+ const filter = this.filter;
18
+ const options = this.options;
19
+
20
+ options.single = true;
21
+ removeDocuments(coll, filter, options, (err, r) => deleteCallback(err, r, callback));
22
+ }
23
+ }
24
+
25
+ module.exports = DeleteOneOperation;
@@ -0,0 +1,85 @@
1
+ 'use strict';
2
+
3
+ const Aspect = require('./operation').Aspect;
4
+ const defineAspects = require('./operation').defineAspects;
5
+ const CommandOperationV2 = require('./command_v2');
6
+ const decorateWithCollation = require('../utils').decorateWithCollation;
7
+ const decorateWithReadConcern = require('../utils').decorateWithReadConcern;
8
+
9
+ /**
10
+ * Return a list of distinct values for the given key across a collection.
11
+ *
12
+ * @class
13
+ * @property {Collection} a Collection instance.
14
+ * @property {string} key Field of the document to find distinct values for.
15
+ * @property {object} query The query for filtering the set of documents to which we apply the distinct filter.
16
+ * @property {object} [options] Optional settings. See Collection.prototype.distinct for a list of options.
17
+ */
18
+ class DistinctOperation extends CommandOperationV2 {
19
+ /**
20
+ * Construct a Distinct operation.
21
+ *
22
+ * @param {Collection} a Collection instance.
23
+ * @param {string} key Field of the document to find distinct values for.
24
+ * @param {object} query The query for filtering the set of documents to which we apply the distinct filter.
25
+ * @param {object} [options] Optional settings. See Collection.prototype.distinct for a list of options.
26
+ */
27
+ constructor(collection, key, query, options) {
28
+ super(collection, options);
29
+
30
+ this.collection = collection;
31
+ this.key = key;
32
+ this.query = query;
33
+ }
34
+
35
+ /**
36
+ * Execute the operation.
37
+ *
38
+ * @param {Collection~resultCallback} [callback] The command result callback
39
+ */
40
+ execute(server, callback) {
41
+ const coll = this.collection;
42
+ const key = this.key;
43
+ const query = this.query;
44
+ const options = this.options;
45
+
46
+ // Distinct command
47
+ const cmd = {
48
+ distinct: coll.collectionName,
49
+ key: key,
50
+ query: query
51
+ };
52
+
53
+ // Add maxTimeMS if defined
54
+ if (typeof options.maxTimeMS === 'number') {
55
+ cmd.maxTimeMS = options.maxTimeMS;
56
+ }
57
+
58
+ // Do we have a readConcern specified
59
+ decorateWithReadConcern(cmd, coll, options);
60
+
61
+ // Have we specified collation
62
+ try {
63
+ decorateWithCollation(cmd, coll, options);
64
+ } catch (err) {
65
+ return callback(err, null);
66
+ }
67
+
68
+ super.executeCommand(server, cmd, (err, result) => {
69
+ if (err) {
70
+ callback(err);
71
+ return;
72
+ }
73
+
74
+ callback(null, this.options.full ? result : result.values);
75
+ });
76
+ }
77
+ }
78
+
79
+ defineAspects(DistinctOperation, [
80
+ Aspect.READ_OPERATION,
81
+ Aspect.RETRYABLE,
82
+ Aspect.EXECUTE_WITH_SELECTION
83
+ ]);
84
+
85
+ module.exports = DistinctOperation;
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ const Aspect = require('./operation').Aspect;
4
+ const CommandOperation = require('./command');
5
+ const defineAspects = require('./operation').defineAspects;
6
+ const handleCallback = require('../utils').handleCallback;
7
+
8
+ class DropOperation extends CommandOperation {
9
+ constructor(db, options) {
10
+ const finalOptions = Object.assign({}, options, db.s.options);
11
+
12
+ if (options.session) {
13
+ finalOptions.session = options.session;
14
+ }
15
+
16
+ super(db, finalOptions);
17
+ }
18
+
19
+ execute(callback) {
20
+ super.execute((err, result) => {
21
+ if (err) return handleCallback(callback, err);
22
+ if (result.ok) return handleCallback(callback, null, true);
23
+ handleCallback(callback, null, false);
24
+ });
25
+ }
26
+ }
27
+
28
+ defineAspects(DropOperation, Aspect.WRITE_OPERATION);
29
+
30
+ class DropCollectionOperation extends DropOperation {
31
+ constructor(db, name, options) {
32
+ super(db, options);
33
+
34
+ this.name = name;
35
+ this.namespace = `${db.namespace}.${name}`;
36
+ }
37
+
38
+ _buildCommand() {
39
+ return { drop: this.name };
40
+ }
41
+ }
42
+
43
+ class DropDatabaseOperation extends DropOperation {
44
+ _buildCommand() {
45
+ return { dropDatabase: 1 };
46
+ }
47
+ }
48
+
49
+ module.exports = {
50
+ DropOperation,
51
+ DropCollectionOperation,
52
+ DropDatabaseOperation
53
+ };
@@ -0,0 +1,42 @@
1
+ 'use strict';
2
+
3
+ const Aspect = require('./operation').Aspect;
4
+ const defineAspects = require('./operation').defineAspects;
5
+ const CommandOperation = require('./command');
6
+ const applyWriteConcern = require('../utils').applyWriteConcern;
7
+ const handleCallback = require('../utils').handleCallback;
8
+
9
+ class DropIndexOperation extends CommandOperation {
10
+ constructor(collection, indexName, options) {
11
+ super(collection.s.db, options, collection);
12
+
13
+ this.collection = collection;
14
+ this.indexName = indexName;
15
+ }
16
+
17
+ _buildCommand() {
18
+ const collection = this.collection;
19
+ const indexName = this.indexName;
20
+ const options = this.options;
21
+
22
+ let cmd = { dropIndexes: collection.collectionName, index: indexName };
23
+
24
+ // Decorate command with writeConcern if supported
25
+ cmd = applyWriteConcern(cmd, { db: collection.s.db, collection }, options);
26
+
27
+ return cmd;
28
+ }
29
+
30
+ execute(callback) {
31
+ // Execute command
32
+ super.execute((err, result) => {
33
+ if (typeof callback !== 'function') return;
34
+ if (err) return handleCallback(callback, err, null);
35
+ handleCallback(callback, null, result);
36
+ });
37
+ }
38
+ }
39
+
40
+ defineAspects(DropIndexOperation, Aspect.WRITE_OPERATION);
41
+
42
+ module.exports = DropIndexOperation;
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ const Aspect = require('./operation').Aspect;
4
+ const defineAspects = require('./operation').defineAspects;
5
+ const DropIndexOperation = require('./drop_index');
6
+ const handleCallback = require('../utils').handleCallback;
7
+
8
+ class DropIndexesOperation extends DropIndexOperation {
9
+ constructor(collection, options) {
10
+ super(collection, '*', options);
11
+ }
12
+
13
+ execute(callback) {
14
+ super.execute(err => {
15
+ if (err) return handleCallback(callback, err, false);
16
+ handleCallback(callback, null, true);
17
+ });
18
+ }
19
+ }
20
+
21
+ defineAspects(DropIndexesOperation, Aspect.WRITE_OPERATION);
22
+
23
+ module.exports = DropIndexesOperation;
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ const Aspect = require('./operation').Aspect;
4
+ const defineAspects = require('./operation').defineAspects;
5
+ const CommandOperation = require('./command');
6
+ const buildCountCommand = require('./common_functions').buildCountCommand;
7
+ const handleCallback = require('../utils').handleCallback;
8
+
9
+ class EstimatedDocumentCountOperation extends CommandOperation {
10
+ constructor(collection, options) {
11
+ super(collection.s.db, options, collection);
12
+ }
13
+
14
+ _buildCommand() {
15
+ const coll = this.collection;
16
+ let options = this.options;
17
+
18
+ options.collectionName = coll.collectionName;
19
+
20
+ return buildCountCommand(coll, null, options);
21
+ }
22
+
23
+ execute(callback) {
24
+ super.execute((err, result) => {
25
+ if (err) return handleCallback(callback, err);
26
+ handleCallback(callback, null, result.n);
27
+ });
28
+ }
29
+ }
30
+
31
+ defineAspects(EstimatedDocumentCountOperation, Aspect.READ_OPERATION);
32
+
33
+ module.exports = EstimatedDocumentCountOperation;
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ const OperationBase = require('./operation').OperationBase;
4
+ const handleCallback = require('../utils').handleCallback;
5
+ const MongoError = require('../core').MongoError;
6
+ const MongoDBNamespace = require('../utils').MongoDBNamespace;
7
+
8
+ class ExecuteDbAdminCommandOperation extends OperationBase {
9
+ constructor(db, selector, options) {
10
+ super(options);
11
+
12
+ this.db = db;
13
+ this.selector = selector;
14
+ }
15
+
16
+ execute(callback) {
17
+ const db = this.db;
18
+ const selector = this.selector;
19
+ const options = this.options;
20
+
21
+ const namespace = new MongoDBNamespace('admin', '$cmd');
22
+ db.s.topology.command(namespace, selector, options, (err, result) => {
23
+ // Did the user destroy the topology
24
+ if (db.serverConfig && db.serverConfig.isDestroyed()) {
25
+ return callback(new MongoError('topology was destroyed'));
26
+ }
27
+
28
+ if (err) return handleCallback(callback, err);
29
+ handleCallback(callback, null, result.result);
30
+ });
31
+ }
32
+ }
33
+
34
+ module.exports = ExecuteDbAdminCommandOperation;