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

0 votes
Can i know Write a query for a compound index to optimize a search operation in MongoDB.
Feb 22, 2025 in Node-js by Ashutosh
• 33,350 points
541 views

1 answer to this question.

0 votes

A compound index improves search performance by indexing multiple fields.

Query to Create a Compound Index:

db.products.createIndex({ category: 1, price: -1 })

Example Search Using the Index:

db.products.find({ category: "Electronics" }).sort({ price: -1 })

answered Feb 23, 2025 by Kavya

Related Questions In Node-js

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,800 points
10,499 views
0 votes
1 answer

How to check if a collection exists in Mongodb native nodejs driver?

Hello @kartik, The collectionNames method of the native driver's Db object accepts ...READ MORE

answered Nov 27, 2020 in Node-js by Niroj
• 82,800 points
15,479 views
0 votes
1 answer

How to Handle Jest Unit Testing for 'ɵcmp' in a React-in-Angular Hybrid App?

Encountering the 'ɵcmp' property error during Jest ...READ MORE

answered Dec 23, 2024 in Node-js by Navya
671 views
0 votes
1 answer

How do you design a schema for tree structures in MongoDB?

Designing a schema for tree structures in ...READ MORE

answered Feb 22, 2025 in Node-js by Kavya
549 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 to fetch specific fields using MongoDB projections.

Fetching Specific Fields Using MongoDB Projections MongoDB projections ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
539 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