What are stages in MongoDB?
Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!
Stages in MongoDB
In MongoDB, stages are used primarily in the aggregation framework, which processes data in a sequence of steps (stages) to transform or analyze it. Each stage performs a specific operation, and the output of one stage becomes the input for the next.
Common Stages in MongoDB Aggregation
1. $match
- Purpose: Filters documents based on specified conditions.
- Example:
db.collection.aggregate([ { $match: { status: "active" } } ]);
- Use Case: Select documents that meet certain criteria.
2. $project
- Purpose: Reshapes documents by including, excluding, or transforming fields.
- Example:
db.collection.aggregate([ { $project: { name: 1, age: 1, _id: 0 } } ]);
- Use Case: Format the output with specific fields.
3. $group
- Purpose: Groups documents by a specified field and performs aggregations like
sum
,avg
,count
, etc. - Example:
db.collection.aggregate([ { $group: { _id: "$category", totalSales: { $sum: "$sales" } } } ]);
- Use Case: Aggregate data, such as finding totals or averages.
4. $sort
- Purpose: Sorts documents by specified fields in ascending or descending order.
- Example:
db.collection.aggregate([ { $sort: { age: -1 } } ]);
- Use Case: Order data for easier analysis or presentation.
5. $limit
- Purpose: Restricts the number of documents in the output.
- Example:
db.collection.aggregate([ { $limit: 5 } ]);
- Use Case: Return only the top results.
6. $skip
- Purpose: Skips a specified number of documents in the pipeline.
- Example:
db.collection.aggregate([ { $skip: 10 } ]);
- Use Case: Implement pagination by skipping documents.
7. $lookup
- Purpose: Performs a left outer join with another collection.
- Example:
db.orders.aggregate([ { $lookup: { from: "customers", localField: "customerId", foreignField: "_id", as: "customerDetails" } } ]);
- Use Case: Join related data from different collections.
8. $unwind
- Purpose: Deconstructs an array field into multiple documents, one for each array element.
- Example:
db.collection.aggregate([ { $unwind: "$tags" } ]);
- Use Case: Flatten arrays for detailed analysis.
9. $addFields
- Purpose: Adds or modifies fields in documents.
- Example:
db.collection.aggregate([ { $addFields: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } } ]);
- Use Case: Create derived fields.
10. $out
- Purpose: Writes the results of the aggregation pipeline to a new or existing collection.
- Example:
db.collection.aggregate([ { $out: "newCollection" } ]);
- Use Case: Store transformed data.
Summary
Stages in MongoDB’s aggregation framework allow for powerful and flexible data transformation and analysis. They are the building blocks for performing complex queries and deriving insights from your data.
TAGS
Behavioral Interview
CONTRIBUTOR
Design Gurus Team
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.