Web TippsUse custom web fonts in Google Sheets charts(08.09.2026 um 17:05 Uhr)
Web TippsIntroducing the new 1Password App for Google Chat(08.09.2026 um 18:02 Uhr)
Web TippsUse custom web fonts in Google Sheets charts(08.09.2026 um 17:05 Uhr)
Web TippsIntroducing the new 1Password App for Google Chat(08.09.2026 um 18:02 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 3 Min Lesezeit
0

A Guide to MongoDB Operators and Aggregations

↗ Quelle (dev.to)
🗣️ Stimme:

Working with MongoDB, you'll often encounter situations where you need to go beyond basic CRUD operations to filter, transform, and manipulate data in more complex ways. MongoDB's rich set of operators and aggregation framework allows developers to implement powerful queries and data transformations that meet a variety of business needs. In this post, we'll explore MongoDB operators and dive into aggregation pipelines with practical examples.



MongoDB Aggregation Framework

MongoDB's aggregation framework is like a data processing pipeline, where you can define multiple stages that transform and summarize data as it flows through. It’s ideal for performing complex transformations directly within MongoDB, reducing the need to retrieve large datasets and perform processing on the client side.



Let's look at some common aggregation stages, using examples from the provided code.



Example: Age-Based Grouping with $bucket, $sort, and $limit



In this example, we’re grouping documents based on age, dividing them into buckets with age boundaries. We then sort by count (in descending order) and limit the output to the top four groups.




CODE
const result = await eiaDataCollection
.aggregate([
{
$bucket: {
groupBy: "$age", // Grouping field
boundaries: [20, 40, 60, 80],
default: "80 up peoples",
output: {
count: { $sum: 1 },
availablePersons: { $push: "$$ROOT" }
}
}
},
{ $sort: { count: -1 } },
{ $limit: 4 },
{ $project: { count: 1 } }
])
.toArray();








  • $bucket: Divides documents into groups, creating "buckets" based on age.

  • $sort: Orders groups by the count field.

  • $limit: Limits the number of results to four.

  • $project: Specifies fields to include in the final output.



Example: Using $group and $unwind



To explore interests by age, this example first unwinds the interests array in each document, then groups by age and aggregates interests per age.




CODE
const result = await eiaDataCollection
.aggregate([
{ $unwind: "$interests" },
{ $group: { _id: "$age", interestPerAge: { $push: "$interests" } } }
])
.toArray();







  • $unwind: Expands the interests array into individual documents for easier grouping.

  • $group: Groups by age and aggregates interests into an array per age group.



Using $match to Filter Data

If we want only female mentors under 30, we can use $match followed by $project to specify only the fields we need.




CODE
const result = await eiaDataCollection
.aggregate([
{ $match: { gender: "Female", age: { $lt: 30 } } },
{ $project: { name: 1, gender: 1 } }
])
.toArray();







  • $match: Filters documents based on gender and age.

  • $project: Limits the fields in the output to name and gender.



MongoDB Operators in Action



Operators like $set, $push, and $inc are crucial when you need to update documents based on conditions.



Adding a Skill with $push



To add a new skill to a mentor's profile, use $push. This operation appends an object to the skills array.




CODE
const result = await eiaDataCollection.updateOne(
{ email: "[email protected]" },
{
$push: {
skills: {
name: "Python",
level: "Beginner",
isLearning: true
}
}
}
);






Updating a Salary with $inc



To increment a field value, such as a mentor's salary, use $inc. This is useful for cases like applying salary adjustments.




CODE
const result = await eiaDataCollection.updateOne(
{ _id: new ObjectId("6406ad63fc13ae5a40000065") },
{ $inc: { salary: 37 } }
);







Summary

MongoDB’s aggregation framework and operators are versatile tools for data transformation, making it possible to do advanced processing without needing to transfer data to the client side. Aggregations like $group, $bucket, $sort, and $unwind make it possible to reshape your data, while operators such as $push and $inc provide powerful ways to manipulate document fields directly. Understanding and applying these techniques can help you streamline data handling and make your MongoDB applications more efficient.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
Use custom web fonts in Google Sheets charts
2 Quellen
Introducing the new 1Password App for Google Chat
1 Quelle
Context-aware access controls are available for Gemini Enterprise in the Admin console
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten A Guide to MongoDB Operators and Aggregations

Thematisch verwandte Begriffe: Guide, MongoDB, Operators, Aggregations · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...