daviddurika
May 31, 2022

MongoDB aggregations for beginners

MongoDB's aggregation is one of the most powerful tools in the database, allowing you to perform complex data analysis on large datasets. This article will show you how to use aggregation to create powerful data aggregations.

Basic aggregations

An aggregation is a powerful tool for data analysis. It allows you to perform complex queries on datasets. To use the aggregations, you must first have a MongoDB collection. In this example, we'll use a collection of blog posts.

$match stage

Once you have a collection, you can begin creating aggregations. The simplest aggregation is a match, which filters the documents in the collection to those that match the specified criteria.

For example, the following match aggregation will return all documents in the collection where the author is "John Doe":

db.posts.aggregate( [ { $match: { author: "John Doe" } } ] ) 

If you want to return only the documents where the author is "John Doe" and the title contains the word "MongoDB", you can use the following aggregation:

db.posts.aggregate( [ { $match: { author: "John Doe", title: /MongoDB/i } } ] )

The match aggregation is very powerful, but it can only be used to filter documents. If you want to perform complex data analysis, you will need to use other aggregations.

$group stage

The next aggregation is the group. The $group aggregation allows you to group documents by a specific field. For example, the following aggregation will group all documents by the author field:

db.posts.aggregate( [ { $group: { _id: "$author", count: { $sum: 1 } } } ] )

The group aggregation returns a document for each author in the collection. The document contains the author's name and the number of documents written by the author.

Combine multiple stages into a pipelines

If you want to know how many posts each author has written about MongoDB, you can use the following aggregation:

db.posts.aggregate( [ 
  { $match: { title: /MongoDB/i } },
  { $group: { _id: "$author", count: { $sum: 1 } } } 
] )

The match aggregation will filter the documents to only those that contain the word "MongoDB" in the title. The group aggregation will then group the documents by author and return the number of posts each author has written about MongoDB.

The group aggregation is very powerful, but it can only be used to group documents. If you want to perform complex data analysis, you will need to use other aggregations.

$project stage

The final aggregation is the project. The project aggregation allows you to transform the documents in the collection. For example, the following aggregation will return the title and author of each document in the collection:

db.posts.aggregate( [ { $project: { title: 1, author: 1 } } ] )

Aggregation pipeline

MongoDB supports a wide variety of aggregation stages, including those for arithmetic, comparison, and logical operations. See the list of MongoDB aggregation pipeline stages.

In addition to match, the aggregation framework provides a number of other operators, including:

  • $project: This operator allows you to select, rename, and reshape fields in the documents in the collection.
  • $group: This operator groups documents together by a specified key.
  • $sort: This operator sorts the documents in the collection by a specified key.
  • $limit: This operator limits the number of documents that are returned from the aggregation.
  • $skip: This operator skips a specified number of documents.

To learn more about the aggregation framework, check out the MongoDB documentation.

Aggregations made easy with Mingo

Mingo is a great tool for those who are just getting started with MongoDB, and it is also great for those who want a more user-friendly interface for their database. You can easily create MongoDB aggregations with our GUI.

You can learn more about our MongoDB Aggregator Tool or you can download it right away and give it a try.

Read more

Mingo Development Update

We're currently in the middle of a big refactoring and code cleanup effort in Mingo. Our goal is to bring everything up to date, streamline the codebase, improve performance, and set the stage for faster development and new features in the future. Here's what's happening: All this work takes time, but we’re aiming to ship […]
Radoslav Toth

v1.14.1 Released

Mingo is back with a new release packed with 42 improvements, fixes and goodies.
Radoslav Toth

v1.13.5 released

We are regularly releasing new versions and did it again today. The latest release contains several UX improvements, fixes 4 reported bugfixes (thanks!) and 10+ unreported issues we found ourselves. Please keep reporting issues, we appreciate any feedback! Mingo should suggest the update automatically, or you can download it directly from our Download page.
Radoslav Toth

Using Mingo with FerretDB

Mingo is a suitable solution for a GUI for MongoDB and its open-source alternative, FerretDB. Since FerretDB is open source, developers can fully control and adapt it to suit specific needs. FerretDB with Mingo will offer you a visual representation of data and a user-friendly interaction, among other benefits. What is FerretDB FerretDB is the […]
Daniel Njeru

Taking Your Company to New Heights with MongoDB Charts in Mingo

Today we demystify how Mingo can ease your pain as a MongoDB user. Mingo has had positive reviews from all companies that have tried it out. Some users have loved it for its wide range of features that are not in other GUI tools, while others have remained loyal to us due to our favorable […]
Daniel Njeru

How to set up MongoDB charts in Mingo

MongoDB charts are used to create a visual representation of MongoDB data. With MongoDB charts, you can communicate insights clearly and concisely. The feature will be useful when analyzing trends, presenting information, or making data-driven decisions. Complex information is conveyed in a more accessible format. That way, you can focus on developing actionable plans. Mingo […]
Daniel Njeru

Demystifying Data Modeling in MongoDB: A Step-by-Step Approach

Data modeling is the process of defining how data is stored and the relationships that exist among different entities in your data. The organization of data inside a database is referred to as a schema. When developing your schema, put more thoughts on your application than your database. A good data model makes data management […]
Daniel Njeru

Indexing Strategies for High Performance in MongoDB

Indexes are unique data structures that store a small portion of the collection’s data in an ordered form that is easy to traverse and search efficiently. They point to the document identity and allow you to look up, access, and update data faster. In MongoDB, indexes are used to improve query performance. Indexes improve query […]
Daniel Njeru

Which companies are using MongoDB

Those who come bearing advice are rarely popular. Daedalus did not do his son Icarus any favors by warning him not to fly too close to the sun as his wax wings would melt, causing him to fall. Had Icarus not ignored his father’s advice, he would not have plummeted to his death. By all […]
Daniel Njeru

New version released: 1.13.1

Here's a short list of bug fixes and improvements in our latest release. It is definitely not complete, there are many minor goodies we didn't even mention here. Enjoy! Bug Fixes Enhancements We would like to thank all the contributors for their valuable feedback and bug reports. Your contributions help us make Mingo better. Please […]
Radoslav Toth
1 2 3

Download Mingo now

The best MongoDB GUI Admin. Intuitive • Fast • Secure
Download Now