Open In App

Mongoose Document Model.create() API

The Model.create() method of the Mongoose API is used to create single or many documents in the collection. Mongoose by default triggers save() internally when we use the create() method on any model.

Syntax:



Model.create()

Parameters: The Model.create() method accepts three parameters:

Return Value: The Model.create() 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:

 

Example 1: In this example, We have established a database connection using mongoose and defined model over customerSchema, having two columns “name”, and “orderCount“. In the end, we are creating a single document on the Customer model.




// Require mongoose module
const mongoose = require('mongoose');
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
  
// Defining customerSchema schema
const customerSchema = new mongoose.Schema(
    { name: String, orderCount: Number }
)
  
// Defining customerSchema model
const Customer = mongoose.model(
    'Customer', customerSchema);
  
// creating document using create method
Customer.create({ name: 'Rahul', orderCount: 5 })
    .then(result => {
        console.log(result)
    })

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

node app.js

Output:

{
  name: 'Rahul',
  orderCount: 5,
  _id: new ObjectId("6304e68407a431f560473ac2"),
  __v: 0
}

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 customerSchema, having two columns “name”, and “orderCount“. In the end, we are creating multiple documents at a time on the Customer model.




// Require mongoose module
const mongoose = require('mongoose');
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
  
// Defining customerSchema schema
const customerSchema = new mongoose.Schema(
    { name: String, orderCount: Number }
)
  
// Defining customerSchema model
const Customer = mongoose.model(
    'Customer', customerSchema);
  
// Creating document using create method
Customer.create([{ 
    name: 'Customer2'
    orderCount: 10 
},
{ name: 'Customer3', orderCount: 20 }])
   .then(result => {
    console.log(result)
})

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

node app.js

Output:

[
  {
    name: 'Customer2',
    orderCount: 10,
    _id: new ObjectId("6304e7c8c21ca86f5ea6fce3"),
    __v: 0
  },
  {
    name: 'Customer3',
    orderCount: 20,
    _id: new ObjectId("6304e7c8c21ca86f5ea6fce4"),
    __v: 0
  }
]

GUI Representation of the  Database using Robo3T GUI tool

 

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


Article Tags :