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 ...

Get Node.js, MongoDB, and AngularJS Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.