Open In App

Mongoose Query prototype.selected() API

Last Updated : 17 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Mongoose is an Object Data Modeling (ODM) library for MongoDB. It defines a strongly-typed schema, with default values and schema validations which are later mapped to a MongoDB document. 

Mongoose Query API selected method is used to determine whether any selection is made in the Mongoose Query. It gives a Boolean response, i.e, “true” if any selection is made, else “false”. Let’s understand more about this with some examples.

Syntax:

Query.prototype.selected()

Parameters: It does not accept any parameter.

Return type: It returns a Boolean response.

Creating node application And Installing Mongoose:

Step 1: Create a node application using the following command:

mkdir folder_name
cd folder_name
npm init -y
touch main.js

Step 2: After completing the ReactJS application, Install the required module using the following command:

npm install mongoose

Project Structure: It will look like the following.

 

GUI Representation of the  Database using MongoDB Compass: Currently, the collection has no data.

 

Example 1: In this example, we will use the Query API selected() method to check if a selection is made in the Mongoose Query. It will return “true” as we select the “name” property in the documents fetched from the DB.

Filename: main.js

Javascript




const mongoose = require('mongoose')
  
// Database connection
    {
        dbName: 'event_db',
        useNewUrlParser: true,
        useUnifiedTopology: true
    }, err => err ? console.log(err)
        : console.log('Connected to database'));
  
const personSchema = new mongoose.Schema({
    name: {
        type: String,
        select: false
    },
    age: {
        type: Number,
    }
});
  
const personsArray = [
    {
        name: 'Luffy',
        age: 22
    },
    {
        name: 'Nami',
        age: 30
    },
    {
        name: 'Zoro',
        age: 15
    }
]
  
const Person = mongoose.model('Person', personSchema);
  
(async () => {
    const query = Person.find().select('name')
    console.log(query.selected());
})()


Step to Run Application: Run the application using the following command from the root directory of the project:

node main.js

Output: We see that the value remains unchanged in the result.

 

GUI Representation of the Database using MongoDB Compass:

 

Example 2: In this example, we will use the Query API selected() method to check if a selection is made in the Mongoose Query. It will return “false” as we first select the “name” property and then deselect it using the Mongoose Query chaining technique.

Filename: main.js

Javascript




const mongoose = require('mongoose')
  
// Database connection
mongoose.connect(
    {
        dbName: 'event_db',
        useNewUrlParser: true,
        useUnifiedTopology: true
    }, err => err ? console.log(err)
        : console.log('Connected to database'));
  
const personSchema = new mongoose.Schema({
    name: {
        type: String,
        select: false
    },
    age: {
        type: Number,
    }
});
  
const personsArray = [
    {
        name: 'Luffy',
        age: 22
    },
    {
        name: 'Nami',
        age: 30
    },
    {
        name: 'Zoro',
        age: 15
    }
]
  
const Person = mongoose.model('Person', personSchema);
  
(async () => {
    const query = Person.find().select('name').select('-name')
    console.log(query.selected());
})()


Step to Run Application: Run the application using the following command from the root directory of the project:

node main.js

Output: We see that the value remains unchanged in the result.

 

GUI Representation of the  Database using MongoDB Compass:

 

Reference: https://mongoosejs.com/docs/api/query.html#query_Query-selected



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads