Skip to content
Related Articles
Open in App
Not now

Related Articles

Mongoose Document Model.prototype.save() API

Improve Article
Save Article
  • Last Updated : 08 Nov, 2022
Improve Article
Save Article

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 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 model over userSchema, having two columns or fields “name”, and “age”. In the end, we are using save() method on the User model object which will save the update value for 
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,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    age: Number,
});
  
// Defining userSchema model
const 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 model over userSchema, having two columns or fields “name”, and “age”. We have defined a function named “updateDocument” so that we can utilize save() method functionality through 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,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    age: Number,
});
  
// Defining userSchema model
const User = mongoose.model("User", userSchema);
  
const 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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!