Open In App

Mongoose Schema Connection.prototype.models API

Last Updated : 15 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Mongoose Schema API Connection.prototype.models of the Mongoose API is used on the Connection objects. It allows us to get list of all the models we have defined using connection.model() on a particular connection object. Let us understand the models property using an example.

Syntax:

connectionObject.models;

Parameters: This property does not accept any parameter.

Return Value: It returns the list of all the models we have defined using the connection object.

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 cricketerSchema, having three columns or fields “_id”, “name”, and “nationality”. At the end, we are accessing models property on connection object in order to get the details about all the models defined using a particular connection object.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
const connectionObject =
    mongoose.createConnection(
        useNewUrlParser: true,
        useUnifiedTopology: true,
    });
  
const cricketerSchema = new mongoose.Schema({
    _id: Number,
    name: String,
    nationality: String
});
  
const Cricketer =
    connectionObject.model('Cricketer', cricketerSchema);
  
console.log(connectionObject.models);
const modelsArray = Object.keys(connectionObject.models);
console.log(modelsArray)


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

node app.js

Output:

{ Cricketer: Model { Cricketer } }
[ 'Cricketer' ]

Example 2: In this example, we are illustrating the functionality of models property on the connection object.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
const connectionObject =
    mongoose.createConnection(
        useNewUrlParser: true,
        useUnifiedTopology: true,
    });
  
const Cricketer =
    connectionObject.model('Cricketer', new mongoose.Schema({
        _id: Number,
        name: String,
        nationality: String
    }));
  
const IPLTeams =
    connectionObject.model('IPLTeam', new mongoose.Schema({
        name: String,
        nationality: String,
        iplTeam: String
    }));
  
console.log(connectionObject.models);
console.log(Object.keys(connectionObject.models).length)
console.log(Object.keys(connectionObject.models))


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

node app.js

Output:

{ Cricketer: Model { Cricketer }, IPLTeam: Model { IPLTeam } }
2
[ 'Cricketer', 'IPLTeam' ]

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads