Open In App

Mongoose Document.prototype.$isEmpty() API

Last Updated : 09 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Document API.prototype.$isEmpty() method of the Mongoose API is used on the Document model. It allows us verify whether any field in the document is empty or not. If any specific field in the document object is not having value, then the isEmpty() method will return true else it will return false. Let us understand the isEmpty() method using an example.

Syntax:

document.$isEmpty( path )

Parameters: This method accepts a single parameter as described below:

  • path: It is used to specify the path or the field from the collection. It can be of the type String or ArrayString.

Return Value: This method returns Boolean value. If any specific path in the document object does not have any value 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”. At the end, while creating the new object of User model, we have not defined interest field and while using isEmpty() method we can see the output is true for interest field and false for name field.

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: 'Finch', fixedDeposit: 20000, tenure: 20 }
);
  
console.log(newUser.$isEmpty('interest'));
console.log(newUser.$isEmpty('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:

true
false

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”. At the end, while creating the new object of User model using create() method, we have initialized the name field by undefined, later we are accessing isEmpty() method and we can see the expected output.

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: undefined, fixedDeposit: 5000, interest: 0.05, }
)
    .then(document => {
        console.log(document.$isEmpty('name'));
        console.log(document.$isEmpty('interest'));
    });


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
false

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads