You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+15-20Lines changed: 15 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,20 +2,20 @@
2
2
3
3
The official [MongoDB](https://www.mongodb.com/) driver for Node.js.
4
4
5
-
**Upgrading to version 4? Take a look at our [upgrade guide here](https://github.com/mongodb/node-mongodb-native/blob/HEAD/etc/notes/CHANGES_4.0.0.md)!**
5
+
**Upgrading to version 5? Take a look at our [upgrade guide here](https://github.com/mongodb/node-mongodb-native/blob/HEAD/etc/notes/CHANGES_5.0.0.md)!**
@@ -53,7 +53,7 @@ If you run into any unexpected compiler failures against our supported TypeScrip
53
53
54
54
## Installation
55
55
56
-
The recommended way to get started using the Node.js 4.x driver is by using the `npm` (Node Package Manager) to install the dependency in your project.
56
+
The recommended way to get started using the Node.js 5.x driver is by using the `npm` (Node Package Manager) to install the dependency in your project.
57
57
58
58
After you've created your own project using `npm init`, you can run:
This guide will show you how to set up a simple application using Node.js and MongoDB. Its scope is only how to set up the driver and perform the simple CRUD operations. For more in-depth coverage, see the [official documentation](https://docs.mongodb.com/drivers/node/).
@@ -136,18 +135,14 @@ operations using the MongoDB driver.
136
135
137
136
Add code to connect to the server and the database **myProject**:
138
137
139
-
> **NOTE:** All the examples below use async/await syntax.
140
-
>
141
-
> However, all async API calls support an optional callback as the final argument,
142
-
> if a callback is provided a Promise will not be returned.
143
-
144
138
> **NOTE:** Resolving DNS Connection issues
145
139
>
146
140
> Node.js 18 changed the default DNS resolution ordering from always prioritizing ipv4 to the ordering
147
-
> returned by the DNS provider. In some environments, this can result in `localhost` resolving to
141
+
> returned by the DNS provider. In some environments, this can result in `localhost` resolving to
148
142
> an ipv6 address instead of ipv4 and a consequent failure to connect to the server.
149
143
>
150
144
> This can be resolved by:
145
+
>
151
146
> - specifying the ip address family using the MongoClient `family` option (`MongoClient(<uri>, { family: 4 } )`)
152
147
> - launching mongod or mongos with the ipv6 flag enabled ([--ipv6 mongod option documentation](https://www.mongodb.com/docs/manual/reference/program/mongod/#std-option-mongod.--ipv6))
153
148
> - using a host of `127.0.0.1` in place of localhost
@@ -267,7 +262,7 @@ It is our recommendation to use `instanceof` checks on errors and to avoid relyi
267
262
We guarantee `instanceof` checks will pass according to semver guidelines, but errors may be sub-classed or their messages may change at any time, even patch releases, as we see fit to increase the helpfulness of the errors.
268
263
269
264
Any new errors we add to the driver will directly extend an existing error class and no existing error will be moved to a different parent class outside of a major release.
270
-
This means `instanceof` will always be able to accurately capture the errors that our driver throws (or returns in a callback).
265
+
This means `instanceof` will always be able to accurately capture the errors that our driver throws.
@@ -18,6 +18,74 @@ The following is a detailed collection of the changes in the major v5 release of
18
18
19
19
### Optional callback support migrated to `mongodb-legacy`
20
20
21
+
Node v5 drops support for callbacks in favor of a promise-only API. Below are some strategies for
22
+
callback users to adopt driver v5 in order of most recommended to least recommended.
23
+
24
+
The Node driver team understands that a callback to promise migration can be a non-trivial refactor. To help inform your migration strategy, we've outlined three different approaches below.
25
+
26
+
#### Migrate to promise based api (recommended!)
27
+
28
+
The Node team strongly encourages anyone who is able to migrate from callbacks to promises to do so. Adopting the
29
+
regular driver API will streamline the adoption of future driver updates, as well as provide better Typescript support
30
+
than the other options outlined in this document.
31
+
32
+
The promise-based API is identical to the callback API except:
const result =awaitcollection.findOne({ name: 'john snow' });
63
+
/* do something with result */
64
+
} catch (error) {
65
+
/* do something with error */
66
+
}
67
+
```
68
+
69
+
#### Use the promise-based API and `util.callbackify`
70
+
71
+
If you only have a few callback instances where you are currently unable to adopt the promise API, we recommend using the promise API and Nodejs's `callbackify`
72
+
utility to adapt the promise-based API to use callbacks.
73
+
74
+
**Note** Manually converting a promise-based api to a callback-based API is error prone. We strongly encourage the use of `callbackify`.
75
+
76
+
We recommend using callbackify with an anonymous function that has the same signature as the collection
#### Add `mongodb-legacy` as a dependency and update imports to use `mongodb-legacy`
88
+
21
89
If you are a callback user and you are not ready to use promises, support for your workflow has **not** been removed.
22
90
We have migrated it to a new package:
23
91
@@ -26,7 +94,11 @@ We have migrated it to a new package:
26
94
27
95
The package wraps all of the driver's asynchronous operations that previously supported both promises and callbacks. All the wrapped APIs offer callback support via an optional callback argument alongside a Promise return value so projects with mixed usage will continue to work.
28
96
29
-
#### Example usage of equivalent callback and promise usage
97
+
`mongodb-legacy` is intended to preserve driver v4 behavior to enable a smoother transition between
98
+
driver v4 and v5. However, new features will **only** only support a promise-based
99
+
API in both the driver **and** the legacy driver.
100
+
101
+
##### Example usage of equivalent callback and promise usage
30
102
31
103
After installing the package and modifying imports the following example demonstrates equivalent usages of either `async`/`await` syntax, `.then`/`.catch` chaining, or callbacks:
The `mapReduce` helper has been removed from the `Collection` class. The `mapReduce` operation has been
118
-
deprecated in favor of the aggregation pipeline since MongoDB server version 5.0. It is recommended
189
+
The `mapReduce` helper has been removed from the `Collection` class. The `mapReduce` operation has been
190
+
deprecated in favor of the aggregation pipeline since MongoDB server version 5.0. It is recommended
119
191
to migrate code that uses `Collection.mapReduce` to use the aggregation pipeline (see [Map-Reduce to Aggregation Pipeline](https://www.mongodb.com/docs/manual/reference/map-reduce-to-aggregation-pipeline/)).
120
192
121
193
If the `mapReduce` command must be used, the `Db.command()` helper can be used to run the raw
@@ -126,13 +198,17 @@ If the `mapReduce` command must be used, the `Db.command()` helper can be used t
126
198
const collection =db.collection('my-collection');
127
199
128
200
awaitcollection.mapReduce(
129
-
function() { emit(this.user_id, 1); },
130
-
function(k, vals) { return1 },
201
+
function () {
202
+
emit(this.user_id, 1);
203
+
},
204
+
function (k, vals) {
205
+
return1;
206
+
},
131
207
{
132
208
out: 'inline',
133
209
readConcern: 'majority'
134
210
}
135
-
)
211
+
);
136
212
137
213
// manually running the command using `db.command()`
138
214
const command = {
@@ -141,7 +217,7 @@ const command = {
141
217
reduce: 'function(k,vals) { return 1; }',
142
218
out: 'inline',
143
219
readConcern: 'majority'
144
-
}
220
+
};
145
221
146
222
awaitdb.command(command);
147
223
```
@@ -155,7 +231,7 @@ The `digestPassword` option has been removed from the add user helper.
155
231
156
232
### Removal of Internal Types from Public API
157
233
158
-
The following types are used internally the driver but were accidentally exported. They have now been
234
+
The following types are used internally the driver but were accidentally exported. They have now been
159
235
marked internal and are no longer exported.
160
236
161
237
- ServerSelector
@@ -176,7 +252,7 @@ For clarity the deprecated and duplicate export ObjectID has been removed. Objec
176
252
177
253
### `Projection` and `ProjectionOperations` Types Removed
178
254
179
-
Both of these types were unused but exported. These types have been removed. Please
255
+
Both of these types were unused but exported. These types have been removed. Please
The `insert` method accepted an array of documents for multi-document inserts and a single document for single document inserts. `insertOne` should now be used for single-document inserts and `insertMany` should be used for multi-document inserts.
352
+
The `insert` method accepted an array of documents for multi-document inserts and a single document for single document inserts. `insertOne` should now be used for single-document inserts and `insertMany` should be used for multi-document inserts.
0 commit comments