Open In App

Mongoose Document API .prototype.isDirectSelected() Function

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

The Mongoose Document API.prototype.isDirectSelected() method of the Mongoose API is used on the Document model. It allows us to check whether particular path or field from the collection is explicitly selected or not. Let us understand isDirectedSelected() method using an example.

Syntax:

document.isDirectSelected( path );

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

  • path: It is used to specify the path name which we want to check is it selected in the result set.

Return Value: It returns a Boolean value. If the path we provide to the method is explicitly present in the result set than, it will return true else false.

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 cricketerSchema, having three columns or fields “_id”, “name”, and “nationality”. We have selected name field to be included in the result set. At the end, we are calling isDirectSelected() method to verify whether name field is selected in the result set or not.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const cricketerSchema = new mongoose.Schema({
    _id: Number,
    name: String,
    nationality: String
});
  
const Cricketer = 
    mongoose.model('Cricketers', cricketerSchema);
  
Cricketer.findOne().select('name').then(result => {
    console.log(result.isDirectSelected('name'));
    console.log(result.isDirectSelected('nationality'));
})


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 are illustrating the functionality of isDirectSelected() method by passing multiple fields to be selected from the result set.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const cricketerSchema = new mongoose.Schema({
    _id: Number,
    name: String,
    nationality: String
});
  
const Cricketer =
    mongoose.model('Cricketers', cricketerSchema);
  
(async () => {
    const document = 
        await Cricketer.findOne().select(['_id', 'name']);
    console.log(document.isDirectSelected('_id'));
    console.log(document.isDirectSelected('name'));
    console.log(document);
})();


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
true
{ _id: 1, name: 'Ben Stokes' }

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads