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

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

Compare and sync entire MongoDB databases 

Do you ever need to download the production DB to your localhost for development purposes? Or just sync changes from one DB to another? This can be done using Mingo’s Compare & Sync tool. You don’t need to write scripts or use the terminal mongodump / mongorestore functions, anymore. With Mingo’s Compare & Sync tool […]
Radoslav Toth

Mingo’s keyboard shortcuts

We are all tired of new shortcuts we have to learn. Mingo’s shortcuts mimic what you are already used to from a browser or your dev tool. However, Mingo also has some new intuitive “hovering” shortcuts. Just hover over a document and press space or X to select the document. Or hover a document and CMD+C / CTRL+C to copy document value.  To […]
Radoslav Toth

Smart search

The majority of queries we execute on MongoDB collections are very simple. We usually search for an _id, an email or username and we do this many times over every day. To make such searches simple and fast, Mingo provides a few great hacks for you. Value as query First of all, indexed fields can […]
Radoslav Toth

Data relations in Mingo

When documents reference each other, they usually do so by the _id of the referenced document. There are several types of document relations (such as one-to-many, one-to-one, many-to-one, many-to-many), but we can generally say that there is always a parent document (the one referencing) and a child document (the one being referenced). For example, you […]
Radoslav Toth

5 best MongoDB GUI in 2022

What is a MongoDB GUI client? MongoDB GUI is a graphical interface for MongoDB. It provides a way to view and manipulate data in MongoDB databases using the MongoDB API or MongoDB Shell. As you may know, you can query your data through the command line, but this gets messy and you can get easily […]
daviddurika

Download Mingo now

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