Connecting to MongoDB via a Connection String

Another option for connecting to MongoDB using MongoClient is to use a connection string. With this option, you use a simple connection string to create, open, and authenticate a connection to the database. The Server and MongoClient options used to connect to the database are created in the background. This method has the advantage of being very simple to implement.

To connect to MongoDB via a connection string, you need to call the connect() method on the MongoClient class; no instance is required. The connect() method uses the following syntax:

MongoClient.connect(connString, options, callback)

The connString string has the following syntax (described in Table 13.4 ...

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.