Note: You are currently viewing version 2.2 of the Node.js driver documentation.
Click here for the latest version.
- Reference
- ECMAScript 6
- Connecting
Connecting
The MongoClient connection method returns a Promise if no callback is passed to it. Below is an example using the co package to run a generator
function, which is one of the most exciting innovations of ECMAScript 6.
var MongoClient = require('mongodb').MongoClient,
co = require('co'),
assert = require('assert');
co(function*() {
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
var db = yield MongoClient.connect(url);
// Close the connection
db.close();
}).catch(function(err) {
console.log(err.stack);
});
The MongoClient.connect
function returns a Promise
that we then execute using the yield
keyword of the generator
function. If an error happens during the MongoClient.connect
the error is caught by co
and can be inspected by attaching a function to the catch
method as shown above.
On this page