mongodb 3.6.4 → 3.6.8

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 (39) hide show
  1. package/HISTORY.md +43 -0
  2. package/README.md +63 -67
  3. package/lib/bulk/common.js +76 -0
  4. package/lib/cmap/connection.js +36 -32
  5. package/lib/collection.js +50 -34
  6. package/lib/core/auth/mongo_credentials.js +5 -5
  7. package/lib/core/auth/scram.js +2 -1
  8. package/lib/core/connection/connect.js +1 -1
  9. package/lib/core/connection/logger.js +1 -0
  10. package/lib/core/connection/msg.js +3 -1
  11. package/lib/core/connection/utils.js +9 -14
  12. package/lib/core/error.js +2 -2
  13. package/lib/core/index.js +1 -1
  14. package/lib/core/sdam/monitor.js +8 -3
  15. package/lib/core/sdam/topology.js +2 -2
  16. package/lib/core/sdam/topology_description.js +24 -6
  17. package/lib/core/tools/smoke_plugin.js +1 -0
  18. package/lib/core/topologies/read_preference.js +2 -1
  19. package/lib/core/topologies/replset.js +1 -1
  20. package/lib/core/uri_parser.js +2 -1
  21. package/lib/core/utils.js +2 -5
  22. package/lib/core/wireprotocol/command.js +10 -5
  23. package/lib/core/wireprotocol/kill_cursors.js +2 -1
  24. package/lib/db.js +9 -3
  25. package/lib/encrypter.js +163 -0
  26. package/lib/gridfs-stream/upload.js +1 -0
  27. package/lib/mongo_client.js +123 -172
  28. package/lib/operations/add_user.js +2 -1
  29. package/lib/operations/bulk_write.js +0 -8
  30. package/lib/operations/connect.js +14 -61
  31. package/lib/operations/execute_operation.js +47 -69
  32. package/lib/operations/find.js +3 -0
  33. package/lib/operations/find_one_and_replace.js +8 -2
  34. package/lib/operations/find_one_and_update.js +8 -3
  35. package/lib/operations/insert_many.js +1 -5
  36. package/lib/operations/operation.js +1 -1
  37. package/lib/utils.js +88 -30
  38. package/lib/write_concern.js +5 -1
  39. package/package.json +22 -22
package/HISTORY.md CHANGED
@@ -2,6 +2,49 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [3.6.8](https://github.com/mongodb/node-mongodb-native/compare/v3.6.7...v3.6.8) (2021-05-21)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * **cmap:** undo flipping of `beforeHandshake` flag for timeout errors ([#2813](https://github.com/mongodb/node-mongodb-native/issues/2813)) ([6e3bab3](https://github.com/mongodb/node-mongodb-native/commit/6e3bab32204ea905ab9b949edccb68556b50d382))
11
+
12
+ ### [3.6.7](https://github.com/mongodb/node-mongodb-native/compare/v3.6.6...v3.6.7) (2021-05-18)
13
+
14
+
15
+ ### Bug Fixes
16
+
17
+ * **docs:** removing incorrect apm docs ([#2793](https://github.com/mongodb/node-mongodb-native/issues/2793)) ([971259a](https://github.com/mongodb/node-mongodb-native/commit/971259a868a8018e90ebc2f28d151eb7af3dd50a))
18
+ * **NODE-3173:** Preserve sort key order for numeric string keys ([#2790](https://github.com/mongodb/node-mongodb-native/issues/2790)) ([730f43a](https://github.com/mongodb/node-mongodb-native/commit/730f43af6d9e53603af998353b720d8161426d8c))
19
+ * **NODE-3176:** handle errors from MessageStream ([#2774](https://github.com/mongodb/node-mongodb-native/issues/2774)) ([f1afcc4](https://github.com/mongodb/node-mongodb-native/commit/f1afcc4efbc41ce436812a6bfa22843e939ab5cf))
20
+ * **NODE-3192:** check clusterTime is defined before access ([#2806](https://github.com/mongodb/node-mongodb-native/issues/2806)) ([6ceace6](https://github.com/mongodb/node-mongodb-native/commit/6ceace6b245c42b8498fb1b13e7c37a97a46946d))
21
+ * **NODE-3252:** state transistion from DISCONNECTED ([#2807](https://github.com/mongodb/node-mongodb-native/issues/2807)) ([5d8f649](https://github.com/mongodb/node-mongodb-native/commit/5d8f6493a0ba4b525434c0868e2ae12315b4c249))
22
+ * **sdam:** topology no longer causes close event ([#2791](https://github.com/mongodb/node-mongodb-native/issues/2791)) ([16e7064](https://github.com/mongodb/node-mongodb-native/commit/16e70642f25954a03b91a2c2991cea96b8356de7))
23
+ * invalid case on writeconcern makes skip check fail ([#2773](https://github.com/mongodb/node-mongodb-native/issues/2773)) ([b1363c2](https://github.com/mongodb/node-mongodb-native/commit/b1363c26db5da5003f9db43be7e8d6e9007d45bd))
24
+
25
+ ### [3.6.6](https://github.com/mongodb/node-mongodb-native/compare/v3.6.5...v3.6.6) (2021-04-06)
26
+
27
+
28
+ ### Bug Fixes
29
+
30
+ * always close gridfs upload stream on finish ([#2758](https://github.com/mongodb/node-mongodb-native/issues/2758)) ([c976a01](https://github.com/mongodb/node-mongodb-native/commit/c976a01bf385941bee07fa7f021adf1d425109a8))
31
+ * **csfle:** ensure that monitoring connections are not encrypted ([#2749](https://github.com/mongodb/node-mongodb-native/issues/2749)) ([86bddf1](https://github.com/mongodb/node-mongodb-native/commit/86bddf1ef516d6b8c752082e33c15624753579ab))
32
+ * ensure cursor readPreference is applied to find operations ([#2751](https://github.com/mongodb/node-mongodb-native/issues/2751)) ([91ba19e](https://github.com/mongodb/node-mongodb-native/commit/91ba19efdc4713903584c6161cfdd7b91b0e61f9))
33
+ * ensure monitor has rtt pinger in when calculating rtt ([#2757](https://github.com/mongodb/node-mongodb-native/issues/2757)) ([b94519b](https://github.com/mongodb/node-mongodb-native/commit/b94519ba894b4442d3dabbac59bd12784d8b7178))
34
+ * no infinite loop on windows requiring optional deps ([f2a4ff8](https://github.com/mongodb/node-mongodb-native/commit/f2a4ff870178fbbe8de616c45891368665f29f4b))
35
+ * **NODE-2995:** Add shared metadata MongoClient ([#2760](https://github.com/mongodb/node-mongodb-native/issues/2760)) ([9256242](https://github.com/mongodb/node-mongodb-native/commit/9256242d51c037059c0af5ada9639fc0a74ad033))
36
+ * **NODE-3109:** prevent servername from being IP ([#2763](https://github.com/mongodb/node-mongodb-native/issues/2763)) ([312ffef](https://github.com/mongodb/node-mongodb-native/commit/312ffef18c66a0020f19bdc1d654987d9148d709))
37
+
38
+ ### [3.6.5](https://github.com/mongodb/node-mongodb-native/compare/v3.6.4...v3.6.5) (2021-03-16)
39
+
40
+
41
+ ### Bug Fixes
42
+
43
+ * MongoError circular dependency warning ([#2734](https://github.com/mongodb/node-mongodb-native/issues/2734)) ([d67ffa7](https://github.com/mongodb/node-mongodb-native/commit/d67ffa7a2e3f86734c7e9b6944aab1d765b9e75e))
44
+ * move session support check to operation layer ([#2739](https://github.com/mongodb/node-mongodb-native/issues/2739)) ([8b370a7](https://github.com/mongodb/node-mongodb-native/commit/8b370a7ad784f5759c964cdfaec62e06c896dc95))
45
+ * session support detection spec compliance ([#2732](https://github.com/mongodb/node-mongodb-native/issues/2732)) ([9baec71](https://github.com/mongodb/node-mongodb-native/commit/9baec7128f612f2d9c290c85d24e33602f911499))
46
+ * use emitWarning API for internal messages ([#2743](https://github.com/mongodb/node-mongodb-native/issues/2743)) ([8bd9777](https://github.com/mongodb/node-mongodb-native/commit/8bd9777b0aedd56b81675c3e79fae63432319982))
47
+
5
48
  ### [3.6.4](https://github.com/mongodb/node-mongodb-native/compare/v3.6.3...v3.6.4) (2021-02-02)
6
49
 
7
50
 
package/README.md CHANGED
@@ -1,21 +1,24 @@
1
- [![npm](https://nodei.co/npm/mongodb.png?downloads=true&downloadRank=true)](https://nodei.co/npm/mongodb/) [![npm](https://nodei.co/npm-dl/mongodb.png?months=6&height=3)](https://nodei.co/npm/mongodb/)
1
+ # MongoDB NodeJS Driver
2
2
 
3
- [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mongodb/node-mongodb-native?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
3
+ [![npm](https://nodei.co/npm/mongodb.png?downloads=true&downloadRank=true)](https://nodei.co/npm/mongodb/)
4
4
 
5
- # Description
5
+ The official [MongoDB](https://www.mongodb.com/) driver for Node.js.
6
6
 
7
- The official [MongoDB](https://www.mongodb.com/) driver for Node.js. Provides a high-level API on top of [mongodb-core](https://www.npmjs.com/package/mongodb-core) that is meant for end users.
7
+ **NOTE: v3.x released with breaking API changes. You can find a list of changes [here](CHANGES_3.0.0.md).**
8
8
 
9
- **NOTE: v3.x was recently released with breaking API changes. You can find a list of changes [here](CHANGES_3.0.0.md).**
9
+ ## Version 4.0
10
10
 
11
- ## MongoDB Node.JS Driver
11
+ **Looking for the latest?** We're working on the next major version of the driver, now in beta.
12
+ Check out our [beta version 4.0 here](https://github.com/mongodb/node-mongodb-native/tree/4.0), which includes a full migration of the driver to TypeScript.
12
13
 
13
- | what | where |
14
- |---------------|------------------------------------------------|
15
- | documentation | http://mongodb.github.io/node-mongodb-native |
16
- | api-doc | http://mongodb.github.io/node-mongodb-native/3.6/api |
17
- | source | https://github.com/mongodb/node-mongodb-native |
18
- | mongodb | http://www.mongodb.org |
14
+ ## Quick Links
15
+
16
+ | what | where |
17
+ | ------------- | ---------------------------------------------------- |
18
+ | documentation | http://mongodb.github.io/node-mongodb-native |
19
+ | api-doc | http://mongodb.github.io/node-mongodb-native/3.6/api |
20
+ | source | https://github.com/mongodb/node-mongodb-native |
21
+ | mongodb | http://www.mongodb.org |
19
22
 
20
23
  ### Bugs / Feature Requests
21
24
 
@@ -41,12 +44,12 @@ Change history can be found in [`HISTORY.md`](HISTORY.md).
41
44
 
42
45
  For version compatibility matrices, please refer to the following links:
43
46
 
44
- * [MongoDB](https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-mongodb-node)
45
- * [NodeJS](https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-language-node)
47
+ - [MongoDB](https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-mongodb-node)
48
+ - [NodeJS](https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-language-node)
46
49
 
47
- # Installation
50
+ ## Installation
48
51
 
49
- The recommended way to get started using the Node.js 3.0 driver is by using the `npm` (Node Package Manager) to install the dependency in your project.
52
+ The recommended way to get started using the Node.js driver is by using `npm` (Node Package Manager) to install the dependency in your project.
50
53
 
51
54
  ## MongoDB Driver
52
55
 
@@ -64,10 +67,10 @@ You can also use the [Yarn](https://yarnpkg.com/en) package manager.
64
67
 
65
68
  The MongoDB driver depends on several other packages. These are:
66
69
 
67
- * [mongodb-core](https://github.com/mongodb-js/mongodb-core)
68
- * [bson](https://github.com/mongodb/js-bson)
69
- * [kerberos](https://github.com/mongodb-js/kerberos)
70
- * [node-gyp](https://github.com/nodejs/node-gyp)
70
+ - [bson](https://github.com/mongodb/js-bson)
71
+ - [bson-ext](https://github.com/mongodb-js/bson-ext)
72
+ - [kerberos](https://github.com/mongodb-js/kerberos)
73
+ - [mongodb-client-encryption](https://github.com/mongodb/libmongocrypt#readme)
71
74
 
72
75
  The `kerberos` package is a C++ extension that requires a build environment to be installed on your system. You must be able to build Node.js itself in order to compile and install the `kerberos` module. Furthermore, the `kerberos` module requires the MIT Kerberos package to correctly compile on UNIX operating systems. Consult your UNIX operation system package manager for what libraries to install.
73
76
 
@@ -108,9 +111,9 @@ This will print out all the steps npm is performing while trying to install the
108
111
 
109
112
  A compiler tool chain known to work for compiling `kerberos` on Windows is the following.
110
113
 
111
- * Visual Studio C++ 2010 (do not use higher versions)
112
- * Windows 7 64bit SDK
113
- * Python 2.7 or higher
114
+ - Visual Studio C++ 2010 (do not use higher versions)
115
+ - Windows 7 64bit SDK
116
+ - Python 2.7 or higher
114
117
 
115
118
  Open the Visual Studio command prompt. Ensure `node.exe` is in your path and install `node-gyp`.
116
119
 
@@ -168,7 +171,7 @@ For complete MongoDB installation instructions, see [the manual](https://docs.mo
168
171
 
169
172
  1. Download the right MongoDB version from [MongoDB](https://www.mongodb.org/downloads)
170
173
  2. Create a database directory (in this case under **/data**).
171
- 3. Install and start a ``mongod`` process.
174
+ 3. Install and start a `mongod` process.
172
175
 
173
176
  ```bash
174
177
  mongod --dbpath=/data
@@ -192,11 +195,11 @@ const url = 'mongodb://localhost:27017';
192
195
 
193
196
  // Database Name
194
197
  const dbName = 'myproject';
195
-
198
+ const client = new MongoClient(url);
196
199
  // Use connect method to connect to the server
197
- MongoClient.connect(url, function(err, client) {
200
+ client.connect(function(err) {
198
201
  assert.equal(null, err);
199
- console.log("Connected successfully to server");
202
+ console.log('Connected successfully to server');
200
203
 
201
204
  const db = client.db(dbName);
202
205
 
@@ -222,23 +225,21 @@ const insertDocuments = function(db, callback) {
222
225
  // Get the documents collection
223
226
  const collection = db.collection('documents');
224
227
  // Insert some documents
225
- collection.insertMany([
226
- {a : 1}, {a : 2}, {a : 3}
227
- ], function(err, result) {
228
+ collection.insertMany([{ a: 1 }, { a: 2 }, { a: 3 }], function(err, result) {
228
229
  assert.equal(err, null);
229
230
  assert.equal(3, result.result.n);
230
231
  assert.equal(3, result.ops.length);
231
- console.log("Inserted 3 documents into the collection");
232
+ console.log('Inserted 3 documents into the collection');
232
233
  callback(result);
233
234
  });
234
- }
235
+ };
235
236
  ```
236
237
 
237
238
  The **insert** command returns an object with the following fields:
238
239
 
239
- * **result** Contains the result document from MongoDB
240
- * **ops** Contains the documents inserted with added **_id** fields
241
- * **connection** Contains the connection used to perform the insert
240
+ - **result** Contains the result document from MongoDB
241
+ - **ops** Contains the documents inserted with added **\_id** fields
242
+ - **connection** Contains the connection used to perform the insert
242
243
 
243
244
  Add the following code to call the **insertDocuments** function:
244
245
 
@@ -255,7 +256,7 @@ const dbName = 'myproject';
255
256
  // Use connect method to connect to the server
256
257
  MongoClient.connect(url, function(err, client) {
257
258
  assert.equal(null, err);
258
- console.log("Connected successfully to server");
259
+ console.log('Connected successfully to server');
259
260
 
260
261
  const db = client.db(dbName);
261
262
 
@@ -289,11 +290,11 @@ const findDocuments = function(db, callback) {
289
290
  // Find some documents
290
291
  collection.find({}).toArray(function(err, docs) {
291
292
  assert.equal(err, null);
292
- console.log("Found the following records");
293
- console.log(docs)
293
+ console.log('Found the following records');
294
+ console.log(docs);
294
295
  callback(docs);
295
296
  });
296
- }
297
+ };
297
298
  ```
298
299
 
299
300
  This query returns all the documents in the **documents** collection. Add the **findDocument** method to the **MongoClient.connect** callback:
@@ -311,7 +312,7 @@ const dbName = 'myproject';
311
312
  // Use connect method to connect to the server
312
313
  MongoClient.connect(url, function(err, client) {
313
314
  assert.equal(null, err);
314
- console.log("Connected correctly to server");
315
+ console.log('Connected correctly to server');
315
316
 
316
317
  const db = client.db(dbName);
317
318
 
@@ -332,16 +333,16 @@ const findDocuments = function(db, callback) {
332
333
  // Get the documents collection
333
334
  const collection = db.collection('documents');
334
335
  // Find some documents
335
- collection.find({'a': 3}).toArray(function(err, docs) {
336
+ collection.find({ a: 3 }).toArray(function(err, docs) {
336
337
  assert.equal(err, null);
337
- console.log("Found the following records");
338
+ console.log('Found the following records');
338
339
  console.log(docs);
339
340
  callback(docs);
340
341
  });
341
- }
342
+ };
342
343
  ```
343
344
 
344
- Only the documents which match ``'a' : 3`` should be returned.
345
+ Only the documents which match `'a' : 3` should be returned.
345
346
 
346
347
  ### Update a document
347
348
 
@@ -352,14 +353,13 @@ const updateDocument = function(db, callback) {
352
353
  // Get the documents collection
353
354
  const collection = db.collection('documents');
354
355
  // Update document where a is 2, set b equal to 1
355
- collection.updateOne({ a : 2 }
356
- , { $set: { b : 1 } }, function(err, result) {
356
+ collection.updateOne({ a: 2 }, { $set: { b: 1 } }, function(err, result) {
357
357
  assert.equal(err, null);
358
358
  assert.equal(1, result.result.n);
359
- console.log("Updated the document with the field a equal to 2");
359
+ console.log('Updated the document with the field a equal to 2');
360
360
  callback(result);
361
361
  });
362
- }
362
+ };
363
363
  ```
364
364
 
365
365
  The method updates the first document where the field **a** is equal to **2** by adding a new field **b** to the document set to **1**. Next, update the callback function from **MongoClient.connect** to include the update method.
@@ -377,7 +377,7 @@ const dbName = 'myproject';
377
377
  // Use connect method to connect to the server
378
378
  MongoClient.connect(url, function(err, client) {
379
379
  assert.equal(null, err);
380
- console.log("Connected successfully to server");
380
+ console.log('Connected successfully to server');
381
381
 
382
382
  const db = client.db(dbName);
383
383
 
@@ -398,13 +398,13 @@ const removeDocument = function(db, callback) {
398
398
  // Get the documents collection
399
399
  const collection = db.collection('documents');
400
400
  // Delete document where a is 3
401
- collection.deleteOne({ a : 3 }, function(err, result) {
401
+ collection.deleteOne({ a: 3 }, function(err, result) {
402
402
  assert.equal(err, null);
403
403
  assert.equal(1, result.result.n);
404
- console.log("Removed the document with the field a equal to 3");
404
+ console.log('Removed the document with the field a equal to 3');
405
405
  callback(result);
406
406
  });
407
- }
407
+ };
408
408
  ```
409
409
 
410
410
  Add the new method to the **MongoClient.connect** callback function.
@@ -422,7 +422,7 @@ const dbName = 'myproject';
422
422
  // Use connect method to connect to the server
423
423
  MongoClient.connect(url, function(err, client) {
424
424
  assert.equal(null, err);
425
- console.log("Connected successfully to server");
425
+ console.log('Connected successfully to server');
426
426
 
427
427
  const db = client.db(dbName);
428
428
 
@@ -444,18 +444,14 @@ performance. The following function creates an index on the **a** field in the
444
444
 
445
445
  ```js
446
446
  const indexCollection = function(db, callback) {
447
- db.collection('documents').createIndex(
448
- { "a": 1 },
449
- null,
450
- function(err, results) {
451
- console.log(results);
452
- callback();
453
- }
454
- );
447
+ db.collection('documents').createIndex({ a: 1 }, null, function(err, results) {
448
+ console.log(results);
449
+ callback();
450
+ });
455
451
  };
456
452
  ```
457
453
 
458
- Add the ``indexCollection`` method to your app:
454
+ Add the `indexCollection` method to your app:
459
455
 
460
456
  ```js
461
457
  const MongoClient = require('mongodb').MongoClient;
@@ -469,7 +465,7 @@ const dbName = 'myproject';
469
465
  // Use connect method to connect to the server
470
466
  MongoClient.connect(url, function(err, client) {
471
467
  assert.equal(null, err);
472
- console.log("Connected successfully to server");
468
+ console.log('Connected successfully to server');
473
469
 
474
470
  const db = client.db(dbName);
475
471
 
@@ -485,13 +481,13 @@ For more detailed information, see the [tutorials](docs/reference/content/tutori
485
481
 
486
482
  ## Next Steps
487
483
 
488
- * [MongoDB Documentation](http://mongodb.org)
489
- * [Read about Schemas](http://learnmongodbthehardway.com)
490
- * [Star us on GitHub](https://github.com/mongodb/node-mongodb-native)
484
+ - [MongoDB Documentation](http://mongodb.org)
485
+ - [Read about Schemas](http://learnmongodbthehardway.com)
486
+ - [Star us on GitHub](https://github.com/mongodb/node-mongodb-native)
491
487
 
492
488
  ## License
493
489
 
494
490
  [Apache 2.0](LICENSE.md)
495
491
 
496
- © 2009-2012 Christian Amor Kvalheim
492
+ © 2009-2012 Christian Amor Kvalheim
497
493
  © 2012-present MongoDB [Contributors](CONTRIBUTORS.md)
@@ -5,6 +5,7 @@ const MongoError = require('../core').MongoError;
5
5
  const ObjectID = require('../core').BSON.ObjectID;
6
6
  const BSON = require('../core').BSON;
7
7
  const MongoWriteConcernError = require('../core').MongoWriteConcernError;
8
+ const emitWarningOnce = require('../utils').emitWarningOnce;
8
9
  const toError = require('../utils').toError;
9
10
  const handleCallback = require('../utils').handleCallback;
10
11
  const applyRetryableWrites = require('../utils').applyRetryableWrites;
@@ -70,6 +71,45 @@ class BulkWriteResult {
70
71
  this.result = bulkResult;
71
72
  }
72
73
 
74
+ /** Number of documents inserted. */
75
+ get insertedCount() {
76
+ return typeof this.result.nInserted !== 'number' ? 0 : this.result.nInserted;
77
+ }
78
+ /** Number of documents matched for update. */
79
+ get matchedCount() {
80
+ return typeof this.result.nMatched !== 'number' ? 0 : this.result.nMatched;
81
+ }
82
+ /** Number of documents modified. */
83
+ get modifiedCount() {
84
+ return typeof this.result.nModified !== 'number' ? 0 : this.result.nModified;
85
+ }
86
+ /** Number of documents deleted. */
87
+ get deletedCount() {
88
+ return typeof this.result.nRemoved !== 'number' ? 0 : this.result.nRemoved;
89
+ }
90
+ /** Number of documents upserted. */
91
+ get upsertedCount() {
92
+ return !this.result.upserted ? 0 : this.result.upserted.length;
93
+ }
94
+
95
+ /** Upserted document generated Id's, hash key is the index of the originating operation */
96
+ get upsertedIds() {
97
+ const upserted = {};
98
+ for (const doc of !this.result.upserted ? [] : this.result.upserted) {
99
+ upserted[doc.index] = doc._id;
100
+ }
101
+ return upserted;
102
+ }
103
+
104
+ /** Inserted document generated Id's, hash key is the index of the originating operation */
105
+ get insertedIds() {
106
+ const inserted = {};
107
+ for (const doc of !this.result.insertedIds ? [] : this.result.insertedIds) {
108
+ inserted[doc.index] = doc._id;
109
+ }
110
+ return inserted;
111
+ }
112
+
73
113
  /**
74
114
  * Evaluates to true if the bulk operation correctly executes
75
115
  * @type {boolean}
@@ -571,6 +611,35 @@ class BulkWriteError extends MongoError {
571
611
  this.name = 'BulkWriteError';
572
612
  this.result = result;
573
613
  }
614
+
615
+ /** Number of documents inserted. */
616
+ get insertedCount() {
617
+ return this.result.insertedCount;
618
+ }
619
+ /** Number of documents matched for update. */
620
+ get matchedCount() {
621
+ return this.result.matchedCount;
622
+ }
623
+ /** Number of documents modified. */
624
+ get modifiedCount() {
625
+ return this.result.modifiedCount;
626
+ }
627
+ /** Number of documents deleted. */
628
+ get deletedCount() {
629
+ return this.result.deletedCount;
630
+ }
631
+ /** Number of documents upserted. */
632
+ get upsertedCount() {
633
+ return this.result.upsertedCount;
634
+ }
635
+ /** Inserted document generated Id's, hash key is the index of the originating operation */
636
+ get insertedIds() {
637
+ return this.result.insertedIds;
638
+ }
639
+ /** Upserted document generated Id's, hash key is the index of the originating operation */
640
+ get upsertedIds() {
641
+ return this.result.upsertedIds;
642
+ }
574
643
  }
575
644
 
576
645
  /**
@@ -737,15 +806,19 @@ class FindOperators {
737
806
 
738
807
  /**
739
808
  * backwards compatability for deleteOne
809
+ * @deprecated
740
810
  */
741
811
  removeOne() {
812
+ emitWarningOnce('bulk operation `removeOne` has been deprecated, please use `deleteOne`');
742
813
  return this.deleteOne();
743
814
  }
744
815
 
745
816
  /**
746
817
  * backwards compatability for delete
818
+ * @deprecated
747
819
  */
748
820
  remove() {
821
+ emitWarningOnce('bulk operation `remove` has been deprecated, please use `delete`');
749
822
  return this.delete();
750
823
  }
751
824
  }
@@ -1041,6 +1114,9 @@ class BulkOperationBase {
1041
1114
  }
1042
1115
 
1043
1116
  if (op.insertMany) {
1117
+ emitWarningOnce(
1118
+ 'bulk operation `insertMany` has been deprecated; use multiple `insertOne` ops instead'
1119
+ );
1044
1120
  for (let i = 0; i < op.insertMany.length; i++) {
1045
1121
  if (forceServerObjectId !== true && op.insertMany[i]._id == null)
1046
1122
  op.insertMany[i]._id = new ObjectID();
@@ -58,38 +58,9 @@ class Connection extends EventEmitter {
58
58
  /* ignore errors, listen to `close` instead */
59
59
  });
60
60
 
61
- stream.on('close', () => {
62
- if (this.closed) {
63
- return;
64
- }
65
-
66
- this.closed = true;
67
- this[kQueue].forEach(op =>
68
- op.cb(new MongoNetworkError(`connection ${this.id} to ${this.address} closed`))
69
- );
70
- this[kQueue].clear();
71
-
72
- this.emit('close');
73
- });
74
-
75
- stream.on('timeout', () => {
76
- if (this.closed) {
77
- return;
78
- }
79
-
80
- stream.destroy();
81
- this.closed = true;
82
- this[kQueue].forEach(op =>
83
- op.cb(
84
- new MongoNetworkTimeoutError(`connection ${this.id} to ${this.address} timed out`, {
85
- beforeHandshake: this[kIsMaster] == null
86
- })
87
- )
88
- );
89
-
90
- this[kQueue].clear();
91
- this.emit('close');
92
- });
61
+ this[kMessageStream].on('error', error => this.handleIssue({ destroy: error }));
62
+ stream.on('close', () => this.handleIssue({ isClose: true }));
63
+ stream.on('timeout', () => this.handleIssue({ isTimeout: true, destroy: true }));
93
64
 
94
65
  // hook the message stream up to the passed in stream
95
66
  stream.pipe(this[kMessageStream]);
@@ -132,6 +103,39 @@ class Connection extends EventEmitter {
132
103
  this[kLastUseTime] = now();
133
104
  }
134
105
 
106
+ /**
107
+ * @param {{ isTimeout?: boolean; isClose?: boolean; destroy?: boolean | Error }} issue
108
+ */
109
+ handleIssue(issue) {
110
+ if (this.closed) {
111
+ return;
112
+ }
113
+
114
+ if (issue.destroy) {
115
+ this[kStream].destroy(typeof issue.destroy === 'boolean' ? undefined : issue.destroy);
116
+ }
117
+
118
+ this.closed = true;
119
+
120
+ for (const idAndOp of this[kQueue]) {
121
+ const op = idAndOp[1];
122
+ if (issue.isTimeout) {
123
+ op.cb(
124
+ new MongoNetworkTimeoutError(`connection ${this.id} to ${this.address} timed out`, {
125
+ beforeHandshake: this.ismaster == null
126
+ })
127
+ );
128
+ } else if (issue.isClose) {
129
+ op.cb(new MongoNetworkError(`connection ${this.id} to ${this.address} closed`));
130
+ } else {
131
+ op.cb(typeof issue.destroy === 'boolean' ? undefined : issue.destroy);
132
+ }
133
+ }
134
+
135
+ this[kQueue].clear();
136
+ this.emit('close');
137
+ }
138
+
135
139
  destroy(options, callback) {
136
140
  if (typeof options === 'function') {
137
141
  callback = options;