Time for a new MongoDB GUI?

We'll match you for any remaining time on your current plan.
Let's talk
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

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

MongoDB Atlas vs. self-hosted MongoDB quick guide.

It is plain as day that MongoDB has been a popular database solution for many applications in the recent past. One may argue this is due to its flexibility, scalability, and ease to use. However, Organizations are faced with opting for MongoDB Atlas, a fully managed cloud-based database service, or having MongoDB set up and […]

5 proven MongoDB performance optimization techniques

MongoDB is among the most popular NOSQL databases currently. It’s no secret that developers working on high-performance applications ally with MongoDB. It is a document-based database that is highly scalable and hence handles large amounts of data.  MongoDB can handle data ranging from small data sets to large and complex stacks of information. Whether working […]

We released v1.12.1. What's new?

We have just released a new version and I wanted to summarize what we added or changed. There is a long list of bug-fixes and minor improvements, but three points stand out most. Managing company licenses So far, there was no way to manage company licenses in Mingo. Now, if you have a company account […]
Radoslav Toth

Share MongoDB Configurations, Queries, Aggregation pipelines, NodeShell scripts, and more!

Are you tired of sending connection configurations which is a security risk? Or sending queries, pipelines, and scripts to your team members and then keeping the track of the latest version? Do you wish you could simply share your work with your team for faster and more efficient collaboration? Well, we have some great news […]

Tools for MongoDB

Are you tired of constantly struggling to create or update your MongoDB URIs? We know the feeling, and that's why we created the MongoDB URI Builder. This simple, yet powerful tool is designed to help you build or update your MongoDB URIs with ease. And the best part? It's completely free to use! With the […]

Mingo's security

At Mingo, we understand the importance of keeping your data safe and secure, which is why we've implemented two layers of security to ensure that your data is protected at all times. Two layers of security Everything that happens in Mingo, stays in Mingo. None of your data, connection information nor passwords are sent or […]
Radoslav Toth

MongoDB security for beginners

MongoDB security is based on role-based access control (RBAC). It enables users to control access to data based on their roles in the organization. With RBAC, users can be assigned to roles with specific privileges. Each role is granted access to certain resources, such as databases and collections. This ensures that only authorized users can […]

What is MongoDB?

MongoDB is quickly becoming one of the most popular databases in the world. It is a document-oriented database, which means it stores data in documents, rather than in tables like traditional databases. This makes it an ideal choice for applications that require flexibility and scalability. MongoDB is an ideal choice for applications that require fast […]

MongoDB NodeShell

Mingo's NodeShell let's you execute JavaScript code to work with data directly in Mingo. This is NOT MONGO SHELL, though. The command must be a regular async JS function with a return value. This allows complex code with loops, conditions and variable initialization to be executed. This code must be a definition of a async function and […]
Radoslav Toth

Download Mingo now

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