O'Reilly logo

Node.js, MongoDB, and AngularJS Web Development by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Connecting to MongoDB via a Client Object

Using a MongoClient object to connect to MongoDB involves creating an instance of the client, opening a connection to the database, authenticating to the database if necessary, and then handling logout and closure as needed. This method has the advantage of allowing you to manipulate each step of the connection and authentication.

To connect to MongoDB via a MongoClient object, you need to first create an instance of the MongoClient object. The constructor for the MongoClient class takes a Server object as the first parameter and an object specifying the database connection options as the second parameter, as shown below:

MongoClient(Server, options)

Table 13.3 lists the most important database connection ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required