Write a query to fetch specific fields using MongoDB projections

0 votes
Can you help me to Write a query to fetch specific fields using MongoDB projections.
Feb 22, 2025 in Node-js by Ashutosh
• 33,350 points
539 views

1 answer to this question.

0 votes

Fetching Specific Fields Using MongoDB Projections

MongoDB projections allow you to retrieve only the required fields from a document instead of fetching the entire document. This improves query performance by reducing data transfer and memory usage.

Best Approach

Use the find() method with a projection object to specify the required fields.

Syntax:

db.collection.find(query, { field1: 1, field2: 1, _id: 0 })

Example: Fetch Only name and age Fields from the users Collection

db.users.find({}, { name: 1, age: 1, _id: 0 })

answered Feb 23, 2025 by Kavya

Related Questions In Node-js

0 votes
1 answer
0 votes
1 answer

How to write an action creator that returns a function using redux-thunk?

Using redux-thunk, an action creator can return ...READ MORE

answered Mar 18, 2025 in Node-js by Anvi
559 views
0 votes
1 answer

How to create a directory if it doesn't exist using Node.js?

Hello @kartik, Try: var fs = require('fs'); var dir = ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,800 points
7,007 views
0 votes
1 answer

How to Install a local module using npm?

Hello @kartik, This is what worked for me: npm ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,800 points
11,879 views
0 votes
1 answer

How do you model a many-to-many relationship in MongoDB with an example?

In MongoDB, a many-to-many relationship can be ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
558 views
0 votes
1 answer

What is the difference between RDBMS relationships and MongoDB’s data model?

Feature RDBMS (SQL Databases) MongoDB (NoSQL Document Database) Data Structure Tables ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
481 views
0 votes
1 answer
0 votes
1 answer

What are MongoDB data types, and how do you define them in a schema?

MongoDB supports various data types, including: String (String) ...READ MORE

answered Feb 23, 2025 in Node-js by anonymous
516 views
0 votes
1 answer

Write a query to optimize cursor behavior in MongoDB.

To optimize cursor behavior in MongoDB, you ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
440 views
0 votes
1 answer

Write a query for a compound index to optimize a search operation in MongoDB.

A compound index improves search performance by ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
541 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP