Open In App

Mongoose Document Model.prototype.save() API

Improve
Improve
Like Article
Like
Save
Share
Report

The Model.save() method of the Mongoose API is used to save the changes made in the document and update the latest changes in the collection. We can use the save() method on any model object of the mongoose collection.

Syntax:

Model.save()

Parameters: The Model.save() method accepts two 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 Model.save() function returns a promise. The result contains an object of the model.

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 a model over userSchema, having two columns or fields “name”, and “age”. In the end, we are using the save() method on the User model object which will save the updated value for the “age” attribute in the collection.

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

Javascript




// Require mongoose module
const mongoose = require("mongoose");
 
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
 
let userSchema = new mongoose.Schema({
    name: String,
    age: Number,
});
 
// Defining userSchema model
let User = mongoose.model("User", userSchema);
 
User.findById("63203694182cd3c22ea480ff").then(doc => {
    doc.age = 20;
    doc.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("63203694182cd3c22ea480ff"),
      name: 'User1',
      age: 20,
      __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 a model over userSchema, having two columns or fields “name”, and “age”. We have defined a function named “updateDocument” so that we can utilize the save() method functionality through an asynchronous function.

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

Javascript




// Require mongoose module
const mongoose = require("mongoose");
 
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
 
let userSchema = new mongoose.Schema({
    name: String,
    age: Number,
});
 
// Defining userSchema model
let User = mongoose.model("User", userSchema);
 
let updateDocument = async (age) => {
    const doc = await User.findById("63203694182cd3c22ea480ff")
    doc.age = age;
    const result = await doc.save()
    console.log(result)
}
updateDocument(10)


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("63203694182cd3c22ea480ff"),
      name: 'User1',
      age: 10,
      __v: 0
}

GUI Representation of the Database using Robo3T GUI tool:

 

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



Last Updated : 28 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads