Skip to content
Related Articles
Open in App
Not now

Related Articles

Mongoose Document.prototype.$isNew API

Improve Article
Save Article
  • Last Updated : 09 Dec, 2022
Improve Article
Save Article

The Document API.prototype.$isNew property of the Mongoose API is used on the Document model. It allows us verify whether a document is newly created. When a document is create using new keyword that document object is considered as a new document. Using this concept mongoose decides while saving the document which method it should call insertOne() or updateOne(). Let us understand $isNew property using an example.

Syntax:

document.$isNew

Parameters: This property does not accepts any property.

Return Value: This property returns Boolean value. If the document is created using new keyword than, It will return true else false.

Setting up Node.js Mongoose Module:

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 five columns or fields “_id”, “name”, “fixedDeposit”, “interest”, and “tenure”. We are creating new document using new keyword, at the end we are using isNew property on newUser User model object which is created using new keyword.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    fixedDeposit: Number,
    interest: Number,
    tenure: Number
});
  
const User = mongoose.model('User', userSchema);
  
const newUser = new User(
    {
        name: 'Denial', fixedDeposit: 20000,
        interest: 0.025, tenure: 16
    }
);
  
console.log(newUser.isNew);


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

node app.js

Output:

true

Example 2: In this example, we have established a database connection using mongoose and defined model over userSchema, having five columns or fields “_id”, “name”, “fixedDeposit”, “interest”, and “tenure”. We are creating new User model document using create() method, at the end we are accessing isNew property to verify whether this document object is being created using new keyword or not.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    fixedDeposit: Number,
    interest: Number,
    tenure: Number
});
  
const User = mongoose.model('User', userSchema);
  
User.create(
    {
        name: 'Eric', fixedDeposit: 10000,
        interest: 0.01, tenure: 12
    }
)
    .then(newDocument => {
        console.log(newDocument.isNew);
    });


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

node app.js

Output:

false

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!