Open In App

Mongoose Query.prototype.projection() API

Last Updated : 11 Jan, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The Mongoose Query API.prototype.projection() method of the Mongoose API is used on the Query objects. It allows us to set and configure the projection for the query objects. Using this method we can get the existing projection details and also we can remove the existing projection. Let us understand projection() method using an example.

Syntax:

query.projection( arg );

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

  • arg: It is used to set and remove the projection for the query object. It can be specified as an object or null.

Return Value: This method returns the result set with the current projection.

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 database present in the MongoDB.

 

Example 1: The below example illustrates the basic functionality of the Mongoose Connection projection() method. In this example, we have selected name field for the current projection, and using the projection method we are getting the existing projection details in the form of object.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: { type: String },
    age: { type: Number },
    rollNumber: { type: Number },
});
  
const Student = connectionObject.model('Student', studentSchema);
  
(async () => {
    const result = await Student
        .find({ rollNumber: 9 })
        .select('name')
        .projection();
    console.log(result)
})();


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: 1 }

Example 2: The below example illustrates the basic functionality of the Mongoose Connection projection() method. In this example, we have selected age field for current projection and using the projection method we are setting the current projection to configuration to null.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: { type: String },
    age: { type: Number },
    rollNumber: { type: Number },
});
  
const Student = connectionObject.model('Student', studentSchema);
  
(async () => {
    const query = Student
        .find({ rollNumber: 178 })
        .select('age');
    query.projection(null);
    console.log(query.projection());
})();


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

{}

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



Next Article

Similar Reads

three90RightbarBannerImg