Open In App

Mongoose Schema Connection.prototype.model() Function

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

The Mongoose Schema API Connection.prototype.model() of the Mongoose API is used on the Connection objects. It allows us to define new model in the MongoDB database for particular connection object. It is used to define and retrieve models in and from the database. Let us understand models() method using an example.

Syntax:

connectionObject.model( name, schema, collection, options );

Parameters: This method accepts four parameters as discussed below:

  • name: It is used to specify the name of the model.
  • schema: It is used to define the schema for the model.
  • collection: It is used to specify the name of the collection. It is an optional field, if we do not provide the name of the collection mongoose will take it from the model name.
  • options: It is used to define various properties on model.

Return Value: It returns the complete model based on name, schema we defined for it.

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, as of now we do not have any collection defined for database.

 

Example 1: In this example, we have established a database connection using mongoose and defined model over userSchema. At the end, we are using model() method to the create User model, by providing model name, schema object, and collection name in the database.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
  
// Set Up the Database connection
mongoose.connect(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    age: String,
    city: String,
    phone: Number,
    gender: String
});
  
const User = mongoose.model('User', userSchema, 'User');
  
console.log(User);


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

node app.js

Output:

Model { User }

GUI Representation of the Database using Robo3T GUI tool:

 

Example 2: In this example, we are illustrating the functionality of model() method by passing model name and schema object to the model() method.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
  
// Set Up the Database connection
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const customerSchema = new mongoose.Schema({
    name: String,
    items: [],
    amount: Number,
    date: Date
});
  
const Customer =
    connectionObject.model('Customer', customerSchema);
  
console.log(Customer);


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

node app.js

Output:

Model { Customer }

GUI Representation of the Database using Robo3T GUI tool:

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads