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 in Node-js by Ashutosh
• 20,870 points
63 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 by Kavya

Related Questions In Node-js

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,840 points
6,402 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,840 points
10,281 views
0 votes
1 answer

How to write a test which expects an Error to be thrown in Jasmine?

Hello @kartik, Try using an anonymous function instead: expect( ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,840 points
9,765 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 in Node-js by Kavya
69 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 in Node-js by Kavya
47 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 in Node-js by anonymous
86 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 in Node-js by Kavya
70 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 in Node-js by Kavya
57 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