Open In App

Mongoose Document prototype.save() API

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

The API prototype.save() method of the Mongoose API is used to save the document into the collection. If the document is already present in the collection, it will update that with the latest field values or if the document is not present in the collection or database, It will insert a new document into the collection.

Syntax:

doc.save()

Parameters: The prototype.save() method accepts three parameters:

  • options: It is an object with various properties.
  • callback: It is a callback function that will run once execution is completed.

Return Value: The prototype.save() function returns a promise. The result contains objects with fields present in the document.

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 save() method on the User model which will insert a new document into the User collection.

  • 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 userSchema = new mongoose.Schema({
    name: String,
    age: Number
});
  
// Defining userSchema model
const User = mongoose.model("User", userSchema);
  
// Creating object of User model
const newDoc = new User()
  
// Setting field values
newDoc.name = "User4"
newDoc.age = 40
  
newDoc.save().then(result => {
    console.log(result)
})


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

node app.js

Output:

{
  _id: new ObjectId("6320df12a96d852056b0a0ba"),
  name: 'User4',
  age: 40,
  __v: 0
}

Example 2: In this example, we are using save() inside the asynchronous function to insert a new document in the User collection.

  • 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 userSchema = new mongoose.Schema({
    name: String,
    age: Number,
});
  
// Defining userSchema model
const User = mongoose.model("User", userSchema);
  
// save() using async function 
const saveDocument = async () => {
    const newDoc = new User();
    newDoc.name = "User5";
    newDoc.age = 50;
    const output = await newDoc.save();
    console.log(output)
};
  
// Calling async function defined above
saveDocument();


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

node app.js

Output:

{
  _id: new ObjectId("6320e0379cdf11e37f2b0b23"),
  name: 'User5',
  age: 50,
  __v: 0
}

Reference: https://mongoosejs.com/docs/api/document.html#document_Document-save



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

Similar Reads