Open In App

Mongoose Document Model.init() API

Last Updated : 28 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Model.init() method of Mongoose API is responsible for building indexes. Although, Mongoose calls this function automatically when a model is created using mongoose.model() or connection.model().

Syntax:

Model_Name.init()

Parameters: The Model.init() method accepts one parameters:

  • callback: It is a callback function that will run once execution is completed.

Return Value: The Model.init() function returns a promise.

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:

 

Database Structure: The database structure will look like this, the following documents are present in the collection.

 

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 the init() method on the User model which will build the indexes for the model.

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

App.js




// 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);
  
User.init().then(function(Event){
    console.log('Indexes Builded')
})


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

node app.js

Output:

Indexes Builded

GUI Representation of the Database using Robo3T GUI tool:

 

Example 2: In this example, we have established a database connection using mongoose and defined model over studentSchema, having three columns or fields “name”, “school”, and “class”. In this example, we are trying to build the indexes for the student model and listing them in the callback function.

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

App.js




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: String,
    school: String,
    class: Number,
});
  
// Defining studentSchema model
const Student = mongoose.model("Student", studentSchema);
  
Student.init().then(function(Event){
    console.log("Indexes Built successfully")
    console.log(Student.listIndexes())
})


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

node app.js

Output:

Indexes Built successfully
Promise { <pending> }

GUI Representation of the Database using Robo3T GUI tool:

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads