Open In App

Mongoose Schema Connection.prototype.useDb() API

Improve
Improve
Like Article
Like
Save
Share
Report

The Connection.prototype.useDb() method of the Mongoose API is used on the Connection object. It allows us to change the current working database. It is used to switch between databases using the same connection pool. Let us understand useDb() method using an example.

Syntax:

connection.useDb( <db_name> );

Parameters: This method accepts two parameters as described below:

  • name: It is used to specify the name of the database we want to switch to.
  • options: It is used to specify various properties for the new database.

Return Value: This method returns a new connection object with a new database instance.

Setting up Node.js Mongoose Application:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this: 

 

Database Structure: The database structure will look like this, the following database is present in the MongoDB.

 

Example 1: The below example illustrates the basic functionality of the Mongoose Connection useDb() method. The initial, database object we have is geeksforgeeks later at the end, we are using useDb() to switch to the newgeeksforgeeks database. Along with that to verify we are defining the Customer model in the new database using its reference newgeeksforgeeks.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
 
// Set Up the Database connection
 
let connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
 
 
let newgeeksforgeekscon =
    connectionObject.useDb('newgeeksforgeeks');
 
let Customer = newgeeksforgeekscon.
    model('Customer', new mongoose.Schema({
        name: String,
        address: String,
        orderNumber: Number,
    }));


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

GUI Representation of the Database using Robo3T GUI tool:

 

Example 2: The below example illustrates the basic functionality of the Mongoose Connection useDb() method. First, we switch to newgeeksforgeeks database and at the end, we are calling dropDatabase() method on newgeeksforgeekscon object to drop the newgeeksforgeeks database.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
 
// Set Up the Database connection
 
let connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
 
let newgeeksforgeekscon = connectionObject.
    useDb('newgeeksforgeeks');
 
newgeeksforgeekscon.dropDatabase((err, res) => {
    if (err) {
        console.log(err);
    } else {
        console.log(res);
    }
})


Output:

true

GUI Representation of the Database using Robo3T GUI tool:

 

Reference: https://mongoosejs.com/docs/api/connection.html#connection_Connection-useDb



Last Updated : 28 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads