MongoDBTutorial
Connect To A MongoDB Database Using Node.js
Summary
This tutorial walks through connecting a Node.js script to a MongoDB database, listing the cluster’s databases, and printing the results to the console. It begins with prerequisites: a supported Node.js version, the MongoDB Node.js Driver installed via npm, and a free MongoDB Atlas cluster loaded with sample data. The Atlas connection wizard adds the current IP address, creates a MongoDB user, and supplies a connection string for Node.js driver version 3.6 or later. The script imports MongoClient, defines an asynchronous main function, creates a client with the connection URI, and awaits client.connect(). Database calls sit inside try/catch, with a finally block closing the connection, and a listDatabases function retrieves and prints the database names before the script runs with node connection.js.
Context
The post is the first entry in a Quick Start series on using MongoDB with Node.js. It assumes a free Atlas cluster loaded with sample data and notes that the driver installed at the time of writing was version 3.6.4. It also mentions DeprecationWarnings around the URL string parser and Server Discover and Monitoring engine, which can be removed with the useNewUrlParser and useUnifiedTopology options.
Approach / What changed
Install the MongoDB Node.js Driver, create an Atlas cluster and load sample data, add the current IP address and a MongoDB user, then copy the Node.js 3.6-or-later connection string. In connection.js, import MongoClient, define an asynchronous main function, set the URI with the user credentials and sample_airbnb, instantiate MongoClient, and await client.connect(). Call listDatabases inside try/catch, close the client in a finally block, and invoke main with errors logged to the console.
Takeaways
The MongoDB Node.js Driver is installed with npm; at the time of writing the install produced driver version 3.6.4, and npm list mongodb shows the installed version.
The Atlas connection wizard adds the current IP address to the IP Access List, creates a MongoDB user, and provides a connection string when Node.js and driver 3.6 or later are selected.
DeprecationWarnings about the URL string parser and Server Discover and Monitoring engine can be removed by passing useNewUrlParser: true and useUnifiedTopology: true to MongoClient.
FAQ
What prerequisites are needed before connecting Node.js to MongoDB?
A supported Node.js version, the MongoDB Node.js Driver installed via npm, and a MongoDB database. The tutorial uses a free Atlas cluster loaded with sample data and notes the driver requires Node 4.x or greater.
How should the connection be closed if a database call fails?
Wrap database calls in try/catch and put client.close() in a finally block. That ensures the connection to the cluster is closed even when an unexpected error occurs.
What do the useNewUrlParser and useUnifiedTopology options address?
They remove DeprecationWarnings about the URL string parser and the Server Discover and Monitoring engine. The post shows instantiating MongoClient with both options set to true.
Source and freshness
- Source
- mongodb.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.