Mongoose Document.prototype.$isDefault() API
Last Updated :
12 Dec, 2022
The Document API.prototype.$isDefault() method of the Mongoose API is used on the Document model. It allows us to check whether a particular field in Schema has a default value or not. While defining schema we can set the default value for the fields. Let us understand the isDefault() method using an example.
Syntax:
document.$isDefault( path );
Parameters: This method accepts a single parameter as described below:
- path: It is used to specify the path or the field name for which we want to verify isDefault() functionality.
Return Value: This method returns a Boolean value that specifies if the path we provided to the method is set to the default value.
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”. While create new document of User model, we have not assigned any value to tenure field, so that during execution default value gets assign. At the end, we are verifying tenure field is having default value or not using isDefault() method.
Filename: app.js
Javascript
const mongoose = require( "mongoose" );
useNewUrlParser: true ,
useUnifiedTopology: true ,
});
const userSchema = new mongoose.Schema({
name: String,
fixedDeposit: Number,
interest: Number,
tenure: { type: Number, default : 6 }
});
const User = mongoose.model( 'User' , userSchema);
const newUser = new User(
{ name: "Denial" , fixedDeposit: 2000, interest: 0.10 }
);
console.log(newUser);
console.log(newUser.$isDefault( 'tenure' ));
|
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
{
name: 'Denial',
fixedDeposit: 2000,
interest: 0.1,
tenure: 6,
_id: new ObjectId("63917e3942ea51d56bff200a")
}
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”. Another example of isDefault() method,, but in this example we have assigned values to all the fields, that is why we are getting false in output while using isDefault() method for name field.
Filename: app.js
Javascript
const mongoose = require( "mongoose" );
useNewUrlParser: true ,
useUnifiedTopology: true ,
});
const userSchema = new mongoose.Schema({
name: String,
fixedDeposit: Number,
interest: Number,
tenure: { type: Number, default : 6 }
});
const User = mongoose.model( 'User' , userSchema);
User.create(
{
name: "Denial" , fixedDeposit: 2000,
interest: 0.10, tenure: 12
})
.then(newDocument => {
console.log(newDocument);
console.log(newDocument.$isDefault( 'name' ));
});
|
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
{
name: 'Denial',
fixedDeposit: 2000,
interest: 0.1,
tenure: 12,
_id: new ObjectId("639180857bb3df8284caef2b"),
__v: 0
}
false
Reference: https://mongoosejs.com/docs/api/document.html#document_Document-$isDefault
Share your thoughts in the comments
Please Login to comment...