Open In App

Mongoose Connection String Options

Mongoose is a JavaScript library that provides a convenient way to interact with MongoDB databases. In order to use Mongoose, you need to create a connection between your Node.js application and the MongoDB server. The Mongoose connection represents this connection and provides a number of useful features, such as connection pooling and events for monitoring the connection status.

Syntax:



mongoose.connect('<connection-string>', <options>, <callback>);

Parameters: A Mongoose connection string is a URL-like string that contains all the information needed to connect to a MongoDB database using Mongoose. 

 



It typically includes the following parameters:

These options can be passed in as query parameters in the connection string, or they can be specified as an options object passed to the mongoose.connect() method in your Node.js application.

Installation of mongoose module:

Step 1: You can install this package by using this command.

npm install mongoose

Step 2: After installing the mongoose module, you can check your mongoose version in the command prompt using the command.

npm version mongoose

Step 3: After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.

node index.js

Project Structure: The project structure will look like this:

 

Example 1: The below code establishes a connection to a MongoDB database running on localhost with the name Geeksforgeeks. The useNewUrlParser and useUnifiedTopology options are specified to use the new MongoDB connection string parser and Server Discovery and Monitoring engine. The callback function logs a message to the console indicating whether the connection was successful or not.




const mongoose = require("mongoose");
  
const connectionStr = "mongodb://localhost:27017/Geeksforgeeks";
mongoose.set("strictQuery", true);
const options = {
    useNewUrlParser: true,
    useUnifiedTopology: true,
};
  
mongoose.connect(connectionStr, options, (error, connection) => {
    if (error) {
        console.error("Error connecting to MongoDB:", error);
    } else {
        console.log("Connected to MongoDB!");
    }
});

Steps to run the application: Run the index.js file using the below command:

Step 1:  Make sure you have installed the mongoose module using the following command:

npm install mongoose

Step 2: Run the index.js file using the below command:

node index.js

Output:

 

Example 2: In this example, we are using the following Mongoose connection string options: authSource, useNewUrlParser, useUnifiedTopology, connectTimeoutMS, socketTimeoutMS, heartbeatFrequencyMS, retryWrites, w.

If the connection is successful with the given string options then “connected to MongoDB!” will be printed in the console, else error block will run.




const mongoose = require("mongoose");
  
const connectionStr = 
mongoose.set("strictQuery", true);
const options = {
    authSource: "admin",
    useNewUrlParser: true,
    useUnifiedTopology: true,
    connectTimeoutMS: 5000,
    socketTimeoutMS: 20000,
    heartbeatFrequencyMS: 10000,
    retryWrites: true,
    w: "majority",
};
  
mongoose.connect(connectionStr, options, 
(error, connection) => {
    if (error) {
        console.error("Error connecting to MongoDB:", error);
    } else {
        console.log("Connected to MongoDB!");
    }
});

Step to run the application: Run the index.js file using the below command:

node index.js

Output:

 


Article Tags :