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
22
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/ )
44
5- # Description
5+ The official [ MongoDB ] ( https://www.mongodb.com/ ) driver for Node.js.
66
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 ) . **
88
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
1010
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.
1213
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 |
1922
2023### Bugs / Feature Requests
2124
@@ -41,12 +44,12 @@ Change history can be found in [`HISTORY.md`](HISTORY.md).
4144
4245For version compatibility matrices, please refer to the following links:
4346
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 )
4649
47- # Installation
50+ ## Installation
4851
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.
5053
5154## MongoDB Driver
5255
@@ -64,10 +67,10 @@ You can also use the [Yarn](https://yarnpkg.com/en) package manager.
6467
6568The MongoDB driver depends on several other packages. These are:
6669
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 )
7174
7275The ` 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.
7376
@@ -108,9 +111,9 @@ This will print out all the steps npm is performing while trying to install the
108111
109112A compiler tool chain known to work for compiling ` kerberos ` on Windows is the following.
110113
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
114117
115118Open the Visual Studio command prompt. Ensure ` node.exe ` is in your path and install ` node-gyp ` .
116119
@@ -168,7 +171,7 @@ For complete MongoDB installation instructions, see [the manual](https://docs.mo
168171
1691721 . Download the right MongoDB version from [ MongoDB] ( https://www.mongodb.org/downloads )
1701732 . 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.
172175
173176``` bash
174177mongod --dbpath=/data
@@ -192,11 +195,11 @@ const url = 'mongodb://localhost:27017';
192195
193196// Database Name
194197const dbName = ' myproject' ;
195-
198+ const client = new MongoClient (url);
196199// Use connect method to connect to the server
197- MongoClient .connect (url, function (err , client ) {
200+ client .connect (function (err ) {
198201 assert .equal (null , err);
199- console .log (" Connected successfully to server" );
202+ console .log (' Connected successfully to server' );
200203
201204 const db = client .db (dbName);
202205
@@ -222,23 +225,21 @@ const insertDocuments = function(db, callback) {
222225 // Get the documents collection
223226 const collection = db .collection (' documents' );
224227 // 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 ) {
228229 assert .equal (err, null );
229230 assert .equal (3 , result .result .n );
230231 assert .equal (3 , result .ops .length );
231- console .log (" Inserted 3 documents into the collection" );
232+ console .log (' Inserted 3 documents into the collection' );
232233 callback (result);
233234 });
234- }
235+ };
235236```
236237
237238The ** insert** command returns an object with the following fields:
238239
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
242243
243244Add the following code to call the ** insertDocuments** function:
244245
@@ -255,7 +256,7 @@ const dbName = 'myproject';
255256// Use connect method to connect to the server
256257MongoClient .connect (url, function (err , client ) {
257258 assert .equal (null , err);
258- console .log (" Connected successfully to server" );
259+ console .log (' Connected successfully to server' );
259260
260261 const db = client .db (dbName);
261262
@@ -289,11 +290,11 @@ const findDocuments = function(db, callback) {
289290 // Find some documents
290291 collection .find ({}).toArray (function (err , docs ) {
291292 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);
294295 callback (docs);
295296 });
296- }
297+ };
297298```
298299
299300This 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';
311312// Use connect method to connect to the server
312313MongoClient .connect (url, function (err , client ) {
313314 assert .equal (null , err);
314- console .log (" Connected correctly to server" );
315+ console .log (' Connected correctly to server' );
315316
316317 const db = client .db (dbName);
317318
@@ -332,16 +333,16 @@ const findDocuments = function(db, callback) {
332333 // Get the documents collection
333334 const collection = db .collection (' documents' );
334335 // Find some documents
335- collection .find ({' a ' : 3 }).toArray (function (err , docs ) {
336+ collection .find ({ a : 3 }).toArray (function (err , docs ) {
336337 assert .equal (err, null );
337- console .log (" Found the following records" );
338+ console .log (' Found the following records' );
338339 console .log (docs);
339340 callback (docs);
340341 });
341- }
342+ };
342343```
343344
344- Only the documents which match `` 'a' : 3 ` ` should be returned.
345+ Only the documents which match ` 'a' : 3 ` should be returned.
345346
346347### Update a document
347348
@@ -352,14 +353,13 @@ const updateDocument = function(db, callback) {
352353 // Get the documents collection
353354 const collection = db .collection (' documents' );
354355 // 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 ) {
357357 assert .equal (err, null );
358358 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' );
360360 callback (result);
361361 });
362- }
362+ };
363363```
364364
365365The 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';
377377// Use connect method to connect to the server
378378MongoClient .connect (url, function (err , client ) {
379379 assert .equal (null , err);
380- console .log (" Connected successfully to server" );
380+ console .log (' Connected successfully to server' );
381381
382382 const db = client .db (dbName);
383383
@@ -398,13 +398,13 @@ const removeDocument = function(db, callback) {
398398 // Get the documents collection
399399 const collection = db .collection (' documents' );
400400 // Delete document where a is 3
401- collection .deleteOne ({ a : 3 }, function (err , result ) {
401+ collection .deleteOne ({ a: 3 }, function (err , result ) {
402402 assert .equal (err, null );
403403 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' );
405405 callback (result);
406406 });
407- }
407+ };
408408```
409409
410410Add the new method to the ** MongoClient.connect** callback function.
@@ -422,7 +422,7 @@ const dbName = 'myproject';
422422// Use connect method to connect to the server
423423MongoClient .connect (url, function (err , client ) {
424424 assert .equal (null , err);
425- console .log (" Connected successfully to server" );
425+ console .log (' Connected successfully to server' );
426426
427427 const db = client .db (dbName);
428428
@@ -444,18 +444,14 @@ performance. The following function creates an index on the **a** field in the
444444
445445``` js
446446const 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+ });
455451};
456452```
457453
458- Add the `` indexCollection ` ` method to your app:
454+ Add the ` indexCollection ` method to your app:
459455
460456``` js
461457const MongoClient = require (' mongodb' ).MongoClient ;
@@ -469,7 +465,7 @@ const dbName = 'myproject';
469465// Use connect method to connect to the server
470466MongoClient .connect (url, function (err , client ) {
471467 assert .equal (null , err);
472- console .log (" Connected successfully to server" );
468+ console .log (' Connected successfully to server' );
473469
474470 const db = client .db (dbName);
475471
@@ -485,13 +481,13 @@ For more detailed information, see the [tutorials](docs/reference/content/tutori
485481
486482## Next Steps
487483
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 )
491487
492488## License
493489
494490[ Apache 2.0] ( LICENSE.md )
495491
496- © 2009-2012 Christian Amor Kvalheim
492+ © 2009-2012 Christian Amor Kvalheim
497493© 2012-present MongoDB [ Contributors] ( CONTRIBUTORS.md )
0 commit comments