Open In App

Mongoose Document.prototype.isSelected() API

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

The Document API.prototype.isSelected() method of the Mongoose API is used on the Document model. It allows us to check whether specific path or field is selected in the query to be included in the result set or not. It returns a Boolean value.

 Syntax:

document.isSelected( path )

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

  • path: It is used to specify the path or the field we have selected while initializing the query. It can be of the type String or ArrayString.

Return Value: This method returns Boolean value. If we have selected the field which we have provided to isSelected() method, 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 have selected name field to be included in the result set. At the end, we are verifying the name field using isSelected() method.

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.find().select('name').then(document => {
    console.log(document);
    console.log(document[0].isSelected('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:

[
  { _id: new ObjectId("638f0262cc8a382bcf3d93df"), name: 'Bhavesh' },
  { _id: new ObjectId("638f0262cc8a382bcf3d93de"), name: 'Aditya' }  
]
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 have provided an array of fields to be included in the result set. At the end, we are getting false as an output for fields we have passed to isSelected() method, because we have not selected those fields while initializing the query.

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 isSelectedExample2 = async () => {
    const document = await User
        .findOne()
        .select(['name', 'fixedDeposit']);
    console.log(document);
    console.log(document.isSelected(['tenure', 'interest']));
}
isSelectedExample2();


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("638f0262cc8a382bcf3d93df"),
  name: 'Bhavesh',
  fixedDeposit: 8000
}
false

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads