Mongoose Schema Connection.prototype.useDb() API

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 w3wiki later at the end, we are using useDb() to switch to the neww3wiki database. Along with that to verify we are defining the Customer model in the new database using its reference neww3wiki.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
 
// Set Up the Database connection
let URI = "mongodb://localhost:27017/w3wiki"
 
let connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
 
 
let neww3wikicon =
    connectionObject.useDb('neww3wiki');
 
let Customer = neww3wikicon.
    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 neww3wiki database and at the end, we are calling dropDatabase() method on neww3wikicon object to drop the neww3wiki database.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
 
// Set Up the Database connection
let URI = "mongodb://localhost:27017/w3wiki"
 
let connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
 
let neww3wikicon = connectionObject.
    useDb('neww3wiki');
 
neww3wikicon.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



Contact Us