mongodb 3.6.3 → 3.6.7

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 (60) hide show
  1. package/HISTORY.md +63 -1
  2. package/README.md +63 -69
  3. package/lib/admin.js +10 -8
  4. package/lib/aggregation_cursor.js +7 -1
  5. package/lib/bulk/common.js +81 -4
  6. package/lib/cmap/connection.js +36 -32
  7. package/lib/collection.js +151 -89
  8. package/lib/core/auth/mongo_credentials.js +5 -5
  9. package/lib/core/auth/scram.js +2 -1
  10. package/lib/core/connection/connect.js +1 -1
  11. package/lib/core/connection/logger.js +1 -0
  12. package/lib/core/connection/msg.js +3 -1
  13. package/lib/core/connection/utils.js +9 -14
  14. package/lib/core/cursor.js +4 -1
  15. package/lib/core/index.js +1 -1
  16. package/lib/core/sdam/monitor.js +22 -14
  17. package/lib/core/sdam/topology.js +4 -3
  18. package/lib/core/sdam/topology_description.js +24 -6
  19. package/lib/core/tools/smoke_plugin.js +1 -0
  20. package/lib/core/topologies/mongos.js +2 -1
  21. package/lib/core/topologies/read_preference.js +2 -1
  22. package/lib/core/topologies/replset.js +13 -7
  23. package/lib/core/topologies/server.js +3 -2
  24. package/lib/core/uri_parser.js +20 -3
  25. package/lib/core/utils.js +2 -5
  26. package/lib/core/wireprotocol/command.js +10 -5
  27. package/lib/core/wireprotocol/kill_cursors.js +2 -1
  28. package/lib/core/wireprotocol/query.js +12 -11
  29. package/lib/core/wireprotocol/write_command.js +10 -1
  30. package/lib/cursor.js +15 -13
  31. package/lib/db.js +52 -30
  32. package/lib/encrypter.js +163 -0
  33. package/lib/explain.js +55 -0
  34. package/lib/gridfs/grid_store.js +13 -9
  35. package/lib/gridfs-stream/upload.js +6 -3
  36. package/lib/mongo_client.js +123 -170
  37. package/lib/operations/add_user.js +2 -1
  38. package/lib/operations/aggregate.js +5 -7
  39. package/lib/operations/bulk_write.js +0 -8
  40. package/lib/operations/command_v2.js +10 -1
  41. package/lib/operations/common_functions.js +14 -27
  42. package/lib/operations/connect.js +43 -67
  43. package/lib/operations/delete_many.js +15 -2
  44. package/lib/operations/delete_one.js +15 -2
  45. package/lib/operations/distinct.js +10 -2
  46. package/lib/operations/execute_operation.js +47 -69
  47. package/lib/operations/find.js +10 -1
  48. package/lib/operations/find_and_modify.js +14 -1
  49. package/lib/operations/find_one.js +4 -0
  50. package/lib/operations/find_one_and_replace.js +8 -2
  51. package/lib/operations/find_one_and_update.js +8 -3
  52. package/lib/operations/insert_many.js +1 -5
  53. package/lib/operations/map_reduce.js +20 -1
  54. package/lib/operations/operation.js +11 -1
  55. package/lib/operations/update_many.js +23 -2
  56. package/lib/operations/update_one.js +22 -16
  57. package/lib/url_parser.js +19 -14
  58. package/lib/utils.js +110 -32
  59. package/lib/write_concern.js +22 -4
  60. package/package.json +23 -22
package/HISTORY.md CHANGED
@@ -1,7 +1,69 @@
1
- # Change Log
1
+ # Changelog
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.7](https://github.com/mongodb/node-mongodb-native/compare/v3.6.6...v3.6.7) (2021-05-18)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * **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))
11
+ * **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))
12
+ * **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))
13
+ * **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))
14
+ * **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))
15
+ * **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))
16
+ * 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))
17
+
18
+ ### [3.6.6](https://github.com/mongodb/node-mongodb-native/compare/v3.6.5...v3.6.6) (2021-04-06)
19
+
20
+
21
+ ### Bug Fixes
22
+
23
+ * 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))
24
+ * **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))
25
+ * 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))
26
+ * 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))
27
+ * no infinite loop on windows requiring optional deps ([f2a4ff8](https://github.com/mongodb/node-mongodb-native/commit/f2a4ff870178fbbe8de616c45891368665f29f4b))
28
+ * **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))
29
+ * **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))
30
+
31
+ ### [3.6.5](https://github.com/mongodb/node-mongodb-native/compare/v3.6.4...v3.6.5) (2021-03-16)
32
+
33
+
34
+ ### Bug Fixes
35
+
36
+ * MongoError circular dependency warning ([#2734](https://github.com/mongodb/node-mongodb-native/issues/2734)) ([d67ffa7](https://github.com/mongodb/node-mongodb-native/commit/d67ffa7a2e3f86734c7e9b6944aab1d765b9e75e))
37
+ * 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))
38
+ * 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))
39
+ * 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))
40
+
41
+ ### [3.6.4](https://github.com/mongodb/node-mongodb-native/compare/v3.6.3...v3.6.4) (2021-02-02)
42
+
43
+
44
+ ### Features
45
+
46
+ * add explain support ([#2626](https://github.com/mongodb/node-mongodb-native/issues/2626)) ([a827807](https://github.com/mongodb/node-mongodb-native/commit/a8278070992d2de4134dc0841b4027a6cc745a93))
47
+ * Deprecate top-level write concern option keys ([#2624](https://github.com/mongodb/node-mongodb-native/issues/2624)) ([0516d93](https://github.com/mongodb/node-mongodb-native/commit/0516d93f74de4b58a99e8455e59678d4b09cd4a7))
48
+
49
+
50
+ ### Bug Fixes
51
+
52
+ * Allow GridFS write stream to destroy ([#2702](https://github.com/mongodb/node-mongodb-native/issues/2702)) ([b5e9d67](https://github.com/mongodb/node-mongodb-native/commit/b5e9d67d5cd9b1912a349789cf2a122e00a46d1b))
53
+ * awaitable isMaster timeout must respect connectTimeoutMS ([#2627](https://github.com/mongodb/node-mongodb-native/issues/2627)) ([b365c50](https://github.com/mongodb/node-mongodb-native/commit/b365c5061ded832e1682167edac58e8a04b05fc4))
54
+ * don't add empty query string items to connection string ([8897259](https://github.com/mongodb/node-mongodb-native/commit/889725980ec1e3b4be4a74170bea0a3e3d23cf13))
55
+ * don't reset monitor if we aren't streaming topology changes ([a10171b](https://github.com/mongodb/node-mongodb-native/commit/a10171b57d2414f6df2aa8ffe9c2d3938ad838d1))
56
+ * dont parse tls/ssl file paths in uri ([#2718](https://github.com/mongodb/node-mongodb-native/issues/2718)) ([f89e4c1](https://github.com/mongodb/node-mongodb-native/commit/f89e4c1bd59c64664e8c9aa218bcb856be325d34))
57
+ * hasAtomicOperator check respects toBSON transformation ([#2696](https://github.com/mongodb/node-mongodb-native/issues/2696)) ([60936dc](https://github.com/mongodb/node-mongodb-native/commit/60936dca74167de239d1bb51a23cc9870860bdc4))
58
+ * honor ignoreUndefined on findAndModify commands ([#2671](https://github.com/mongodb/node-mongodb-native/issues/2671)) ([a25b67c](https://github.com/mongodb/node-mongodb-native/commit/a25b67c6ac13b6347cb78c4fc56613f3daf44300))
59
+ * ignore ENOTFOUND during TXT record lookup ([2036fe7](https://github.com/mongodb/node-mongodb-native/commit/2036fe7b298b9678e29ede87c1035c748ff89fcd))
60
+ * respect readPreference and writeConcern from connection string ([#2711](https://github.com/mongodb/node-mongodb-native/issues/2711)) ([b657c8c](https://github.com/mongodb/node-mongodb-native/commit/b657c8c4f3f86018cc4824f84cb22e1527d9f9af))
61
+ * restore auto direct connection behavior ([#2719](https://github.com/mongodb/node-mongodb-native/issues/2719)) ([617d9de](https://github.com/mongodb/node-mongodb-native/commit/617d9dec5180c5f7b67bd8c944c168d4cbd27e1c))
62
+ * support empty TXT records in legacy url parser ([2fa5c5f](https://github.com/mongodb/node-mongodb-native/commit/2fa5c5f2a113920baa8e67a1c0d65432690d37fc))
63
+ * transition topology state before async calls ([#2637](https://github.com/mongodb/node-mongodb-native/issues/2637)) ([9df093c](https://github.com/mongodb/node-mongodb-native/commit/9df093c1d46e1f8616c7a979324923205ac3dcd2))
64
+ * **cursor:** don't use other operation's session for cloned cursor operation ([#2705](https://github.com/mongodb/node-mongodb-native/issues/2705)) ([8082c89](https://github.com/mongodb/node-mongodb-native/commit/8082c89f8ef3624d22f4bdd6066b6f72c44f763d))
65
+ * **find:** correctly translate timeout option into noCursorTimeout ([#2700](https://github.com/mongodb/node-mongodb-native/issues/2700)) ([e257e6b](https://github.com/mongodb/node-mongodb-native/commit/e257e6b19d810920bafc579e725e09bd0607b74b))
66
+
5
67
  <a name="3.6.3"></a>
6
68
  ## [3.6.3](https://github.com/mongodb/node-mongodb-native/compare/v3.6.1...v3.6.3) (2020-11-06)
7
69
 
package/README.md CHANGED
@@ -1,23 +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
- [![Build Status](https://secure.travis-ci.org/mongodb/node-mongodb-native.svg?branch=2.1)](http://travis-ci.org/mongodb/node-mongodb-native)
4
- [![Coverage Status](https://coveralls.io/repos/github/mongodb/node-mongodb-native/badge.svg?branch=2.1)](https://coveralls.io/github/mongodb/node-mongodb-native?branch=2.1)
5
- [![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/)
6
4
 
7
- # Description
5
+ The official [MongoDB](https://www.mongodb.com/) driver for Node.js.
8
6
 
9
- 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).**
10
8
 
11
- **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
12
10
 
13
- ## 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.
14
13
 
15
- | what | where |
16
- |---------------|------------------------------------------------|
17
- | documentation | http://mongodb.github.io/node-mongodb-native |
18
- | api-doc | http://mongodb.github.io/node-mongodb-native/3.1/api |
19
- | source | https://github.com/mongodb/node-mongodb-native |
20
- | 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 |
21
22
 
22
23
  ### Bugs / Feature Requests
23
24
 
@@ -43,12 +44,12 @@ Change history can be found in [`HISTORY.md`](HISTORY.md).
43
44
 
44
45
  For version compatibility matrices, please refer to the following links:
45
46
 
46
- * [MongoDB](https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-mongodb-node)
47
- * [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)
48
49
 
49
- # Installation
50
+ ## Installation
50
51
 
51
- 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.
52
53
 
53
54
  ## MongoDB Driver
54
55
 
@@ -66,10 +67,10 @@ You can also use the [Yarn](https://yarnpkg.com/en) package manager.
66
67
 
67
68
  The MongoDB driver depends on several other packages. These are:
68
69
 
69
- * [mongodb-core](https://github.com/mongodb-js/mongodb-core)
70
- * [bson](https://github.com/mongodb/js-bson)
71
- * [kerberos](https://github.com/mongodb-js/kerberos)
72
- * [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)
73
74
 
74
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.
75
76
 
@@ -110,9 +111,9 @@ This will print out all the steps npm is performing while trying to install the
110
111
 
111
112
  A compiler tool chain known to work for compiling `kerberos` on Windows is the following.
112
113
 
113
- * Visual Studio C++ 2010 (do not use higher versions)
114
- * Windows 7 64bit SDK
115
- * 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
116
117
 
117
118
  Open the Visual Studio command prompt. Ensure `node.exe` is in your path and install `node-gyp`.
118
119
 
@@ -170,7 +171,7 @@ For complete MongoDB installation instructions, see [the manual](https://docs.mo
170
171
 
171
172
  1. Download the right MongoDB version from [MongoDB](https://www.mongodb.org/downloads)
172
173
  2. Create a database directory (in this case under **/data**).
173
- 3. Install and start a ``mongod`` process.
174
+ 3. Install and start a `mongod` process.
174
175
 
175
176
  ```bash
176
177
  mongod --dbpath=/data
@@ -194,11 +195,11 @@ const url = 'mongodb://localhost:27017';
194
195
 
195
196
  // Database Name
196
197
  const dbName = 'myproject';
197
-
198
+ const client = new MongoClient(url);
198
199
  // Use connect method to connect to the server
199
- MongoClient.connect(url, function(err, client) {
200
+ client.connect(function(err) {
200
201
  assert.equal(null, err);
201
- console.log("Connected successfully to server");
202
+ console.log('Connected successfully to server');
202
203
 
203
204
  const db = client.db(dbName);
204
205
 
@@ -224,23 +225,21 @@ const insertDocuments = function(db, callback) {
224
225
  // Get the documents collection
225
226
  const collection = db.collection('documents');
226
227
  // Insert some documents
227
- collection.insertMany([
228
- {a : 1}, {a : 2}, {a : 3}
229
- ], function(err, result) {
228
+ collection.insertMany([{ a: 1 }, { a: 2 }, { a: 3 }], function(err, result) {
230
229
  assert.equal(err, null);
231
230
  assert.equal(3, result.result.n);
232
231
  assert.equal(3, result.ops.length);
233
- console.log("Inserted 3 documents into the collection");
232
+ console.log('Inserted 3 documents into the collection');
234
233
  callback(result);
235
234
  });
236
- }
235
+ };
237
236
  ```
238
237
 
239
238
  The **insert** command returns an object with the following fields:
240
239
 
241
- * **result** Contains the result document from MongoDB
242
- * **ops** Contains the documents inserted with added **_id** fields
243
- * **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
244
243
 
245
244
  Add the following code to call the **insertDocuments** function:
246
245
 
@@ -257,7 +256,7 @@ const dbName = 'myproject';
257
256
  // Use connect method to connect to the server
258
257
  MongoClient.connect(url, function(err, client) {
259
258
  assert.equal(null, err);
260
- console.log("Connected successfully to server");
259
+ console.log('Connected successfully to server');
261
260
 
262
261
  const db = client.db(dbName);
263
262
 
@@ -291,11 +290,11 @@ const findDocuments = function(db, callback) {
291
290
  // Find some documents
292
291
  collection.find({}).toArray(function(err, docs) {
293
292
  assert.equal(err, null);
294
- console.log("Found the following records");
295
- console.log(docs)
293
+ console.log('Found the following records');
294
+ console.log(docs);
296
295
  callback(docs);
297
296
  });
298
- }
297
+ };
299
298
  ```
300
299
 
301
300
  This query returns all the documents in the **documents** collection. Add the **findDocument** method to the **MongoClient.connect** callback:
@@ -313,7 +312,7 @@ const dbName = 'myproject';
313
312
  // Use connect method to connect to the server
314
313
  MongoClient.connect(url, function(err, client) {
315
314
  assert.equal(null, err);
316
- console.log("Connected correctly to server");
315
+ console.log('Connected correctly to server');
317
316
 
318
317
  const db = client.db(dbName);
319
318
 
@@ -334,16 +333,16 @@ const findDocuments = function(db, callback) {
334
333
  // Get the documents collection
335
334
  const collection = db.collection('documents');
336
335
  // Find some documents
337
- collection.find({'a': 3}).toArray(function(err, docs) {
336
+ collection.find({ a: 3 }).toArray(function(err, docs) {
338
337
  assert.equal(err, null);
339
- console.log("Found the following records");
338
+ console.log('Found the following records');
340
339
  console.log(docs);
341
340
  callback(docs);
342
341
  });
343
- }
342
+ };
344
343
  ```
345
344
 
346
- Only the documents which match ``'a' : 3`` should be returned.
345
+ Only the documents which match `'a' : 3` should be returned.
347
346
 
348
347
  ### Update a document
349
348
 
@@ -354,14 +353,13 @@ const updateDocument = function(db, callback) {
354
353
  // Get the documents collection
355
354
  const collection = db.collection('documents');
356
355
  // Update document where a is 2, set b equal to 1
357
- collection.updateOne({ a : 2 }
358
- , { $set: { b : 1 } }, function(err, result) {
356
+ collection.updateOne({ a: 2 }, { $set: { b: 1 } }, function(err, result) {
359
357
  assert.equal(err, null);
360
358
  assert.equal(1, result.result.n);
361
- 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');
362
360
  callback(result);
363
361
  });
364
- }
362
+ };
365
363
  ```
366
364
 
367
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.
@@ -379,7 +377,7 @@ const dbName = 'myproject';
379
377
  // Use connect method to connect to the server
380
378
  MongoClient.connect(url, function(err, client) {
381
379
  assert.equal(null, err);
382
- console.log("Connected successfully to server");
380
+ console.log('Connected successfully to server');
383
381
 
384
382
  const db = client.db(dbName);
385
383
 
@@ -400,13 +398,13 @@ const removeDocument = function(db, callback) {
400
398
  // Get the documents collection
401
399
  const collection = db.collection('documents');
402
400
  // Delete document where a is 3
403
- collection.deleteOne({ a : 3 }, function(err, result) {
401
+ collection.deleteOne({ a: 3 }, function(err, result) {
404
402
  assert.equal(err, null);
405
403
  assert.equal(1, result.result.n);
406
- 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');
407
405
  callback(result);
408
406
  });
409
- }
407
+ };
410
408
  ```
411
409
 
412
410
  Add the new method to the **MongoClient.connect** callback function.
@@ -424,7 +422,7 @@ const dbName = 'myproject';
424
422
  // Use connect method to connect to the server
425
423
  MongoClient.connect(url, function(err, client) {
426
424
  assert.equal(null, err);
427
- console.log("Connected successfully to server");
425
+ console.log('Connected successfully to server');
428
426
 
429
427
  const db = client.db(dbName);
430
428
 
@@ -446,18 +444,14 @@ performance. The following function creates an index on the **a** field in the
446
444
 
447
445
  ```js
448
446
  const indexCollection = function(db, callback) {
449
- db.collection('documents').createIndex(
450
- { "a": 1 },
451
- null,
452
- function(err, results) {
453
- console.log(results);
454
- callback();
455
- }
456
- );
447
+ db.collection('documents').createIndex({ a: 1 }, null, function(err, results) {
448
+ console.log(results);
449
+ callback();
450
+ });
457
451
  };
458
452
  ```
459
453
 
460
- Add the ``indexCollection`` method to your app:
454
+ Add the `indexCollection` method to your app:
461
455
 
462
456
  ```js
463
457
  const MongoClient = require('mongodb').MongoClient;
@@ -471,7 +465,7 @@ const dbName = 'myproject';
471
465
  // Use connect method to connect to the server
472
466
  MongoClient.connect(url, function(err, client) {
473
467
  assert.equal(null, err);
474
- console.log("Connected successfully to server");
468
+ console.log('Connected successfully to server');
475
469
 
476
470
  const db = client.db(dbName);
477
471
 
@@ -487,13 +481,13 @@ For more detailed information, see the [tutorials](docs/reference/content/tutori
487
481
 
488
482
  ## Next Steps
489
483
 
490
- * [MongoDB Documentation](http://mongodb.org)
491
- * [Read about Schemas](http://learnmongodbthehardway.com)
492
- * [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)
493
487
 
494
488
  ## License
495
489
 
496
490
  [Apache 2.0](LICENSE.md)
497
491
 
498
- © 2009-2012 Christian Amor Kvalheim
492
+ © 2009-2012 Christian Amor Kvalheim
499
493
  © 2012-present MongoDB [Contributors](CONTRIBUTORS.md)
package/lib/admin.js CHANGED
@@ -166,10 +166,11 @@ Admin.prototype.ping = function(options, callback) {
166
166
  * @param {string} username The username.
167
167
  * @param {string} password The password.
168
168
  * @param {object} [options] Optional settings.
169
- * @param {(number|string)} [options.w] The write concern.
170
- * @param {number} [options.wtimeout] The write concern timeout.
171
- * @param {boolean} [options.j=false] Specify a journal write concern.
172
- * @param {boolean} [options.fsync=false] Specify a file sync write concern.
169
+ * @param {(number|string)} [options.w] **Deprecated** The write concern. Use writeConcern instead.
170
+ * @param {number} [options.wtimeout] **Deprecated** The write concern timeout. Use writeConcern instead.
171
+ * @param {boolean} [options.j=false] **Deprecated** Specify a journal write concern. Use writeConcern instead.
172
+ * @param {boolean} [options.fsync=false] **Deprecated** Specify a file sync write concern. Use writeConcern instead.
173
+ * @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
173
174
  * @param {object} [options.customData] Custom data associated with the user (only Mongodb 2.6 or higher)
174
175
  * @param {object[]} [options.roles] Roles associated with the created user (only Mongodb 2.6 or higher)
175
176
  * @param {ClientSession} [options.session] optional session to use for this operation
@@ -203,10 +204,11 @@ Admin.prototype.addUser = function(username, password, options, callback) {
203
204
  * @method
204
205
  * @param {string} username The username.
205
206
  * @param {object} [options] Optional settings.
206
- * @param {(number|string)} [options.w] The write concern.
207
- * @param {number} [options.wtimeout] The write concern timeout.
208
- * @param {boolean} [options.j=false] Specify a journal write concern.
209
- * @param {boolean} [options.fsync=false] Specify a file sync write concern.
207
+ * @param {(number|string)} [options.w] **Deprecated** The write concern. Use writeConcern instead.
208
+ * @param {number} [options.wtimeout] **Deprecated** The write concern timeout. Use writeConcern instead.
209
+ * @param {boolean} [options.j=false] **Deprecated** Specify a journal write concern. Use writeConcern instead.
210
+ * @param {boolean} [options.fsync=false] **Deprecated** Specify a file sync write concern. Use writeConcern instead.
211
+ * @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
210
212
  * @param {ClientSession} [options.session] optional session to use for this operation
211
213
  * @param {Admin~resultCallback} [callback] The command result callback
212
214
  * @return {Promise} returns Promise if no callback passed
@@ -202,7 +202,7 @@ class AggregationCursor extends Cursor {
202
202
  /**
203
203
  * Add a unwind stage to the aggregation pipeline
204
204
  * @method
205
- * @param {number} field The unwind field name.
205
+ * @param {(string|object)} field The unwind field name or stage document.
206
206
  * @return {AggregationCursor}
207
207
  */
208
208
  unwind(field) {
@@ -322,7 +322,13 @@ AggregationCursor.prototype.get = AggregationCursor.prototype.toArray;
322
322
 
323
323
  /**
324
324
  * Execute the explain for the cursor
325
+ *
326
+ * For backwards compatibility, a verbosity of true is interpreted as "allPlansExecution"
327
+ * and false as "queryPlanner". Prior to server version 3.6, aggregate()
328
+ * ignores the verbosity parameter and executes in "queryPlanner".
329
+ *
325
330
  * @method AggregationCursor.prototype.explain
331
+ * @param {'queryPlanner'|'queryPlannerExtended'|'executionStats'|'allPlansExecution'|boolean} [verbosity=true] - An optional mode in which to run the explain.
326
332
  * @param {AggregationCursor~resultCallback} [callback] The result callback.
327
333
  * @return {Promise} returns Promise if no callback passed
328
334
  */
@@ -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();
@@ -1125,10 +1201,11 @@ class BulkOperationBase {
1125
1201
  * @method
1126
1202
  * @param {WriteConcern} [_writeConcern] Optional write concern. Can also be specified through options.
1127
1203
  * @param {object} [options] Optional settings.
1128
- * @param {(number|string)} [options.w] The write concern.
1129
- * @param {number} [options.wtimeout] The write concern timeout.
1130
- * @param {boolean} [options.j=false] Specify a journal write concern.
1131
- * @param {boolean} [options.fsync=false] Specify a file sync write concern.
1204
+ * @param {(number|string)} [options.w] **Deprecated** The write concern. Use writeConcern instead.
1205
+ * @param {number} [options.wtimeout] **Deprecated** The write concern timeout. Use writeConcern instead.
1206
+ * @param {boolean} [options.j=false] **Deprecated** Specify a journal write concern. Use writeConcern instead.
1207
+ * @param {boolean} [options.fsync=false] **Deprecated** Specify a file sync write concern. Use writeConcern instead.
1208
+ * @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
1132
1209
  * @param {BulkOperationBase~resultCallback} [callback] A callback that will be invoked when bulkWrite finishes/errors
1133
1210
  * @throws {MongoError} Throws error if the bulk object has already been executed
1134
1211
  * @throws {MongoError} Throws error if the bulk object does not have any operations
@@ -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[kIsMaster]
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;