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 )
0 commit comments