Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Mongoose Document Model.prototype.db API

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The Model.prototype.db property of the Mongoose API is used to display the Model-related information in the database. It showcases connections that are being used by models. 

Syntax:

Model_Name.db

Returns: The Model.prototype.db property returns an object with a number of nested objects and properties.

Setting up Node.js 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:

 

Example 1: In this example, We have established a database connection using mongoose and defined model over userSchema, having two columns or fields “name” and “age”. In the end, we are using db property on the User model which will return the connections that are being used b by the User model.

  • app.js: Write down the below code in the app.js file:

Javascript




// Require mongoose module
const mongoose = require('mongoose');
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
  
const userSchema = new mongoose.Schema(
    { name: String, age: Number }
)
  
// Defining userSchema model
const User = mongoose.model('User', userSchema);
  
// Accessing db property on User model
const output = User.db
console.log(output)


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

node app.js

Output:

 

Example 2: In this example, We have established a database connection using mongoose and defined model over studentSchema, having three columns or fields “name”, “father_name”, and “mother_name”. In the end, we are using db property on the Student model which will return the connections that are being used by the Student model.

  • app.js: Write down the below code in the app.js file:

Javascript




// Require mongoose module
const mongoose = require('mongoose');
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
  
const studentSchema = new mongoose.Schema(
    { name: String, father_name: 
      String, mother_name: String }
)
  
// Defining studentSchema model
const Student = mongoose.model('Student', studentSchema);
  
// Accessing db property on  Student model
const output = Student.db
console.log(output)


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

node app.js

Output:

 

Reference: https://mongoosejs.com/docs/api/model.html#model_Model-db


My Personal Notes arrow_drop_up
Last Updated : 07 Sep, 2022
Like Article
Save Article
Similar Reads
Related Tutorials