Aggregation with MongoDB (Comparison with SQL)

By @superoo7-dev3/2/2018utopian-io

In this MongoDB tutorial series, I will explain using aggregate in MongoDB (some comparison with SQL for those who are familiar with SQL). If you are not familiar with MongoDB you can check out the previous tutorial at the bottom (curriculum).


[Source](https://blog.serverdensity.com/mongodb/)

What Will I Learn?

  • Aggregate function in MongoDB
  • Some examples of use cases with Aggregate function
  • Comparison with SQL query language

Requirements

  • MongoDB installed
  • Basic CRUD operation in MongoDB
  • Basic understanding of Node.js and JavaScript
  • Experience in using SQL (for better understanding in this post)

Difficulty

Advanced


Tutorial Contents

What is aggregation?

In a database, when aggregation is carried out, it will process the data records requested and return the desired result. The way it return the result is by grouping the values from the documents which fulfill the condition provided in the query.

In normal SQL, is quite common to have aggregate function with clauses like 'SELECT', 'HAVING', 'GROUP BY', 'SUM', 'AVG' and etc. These aggregate function can be also used in MongoDB just that the syntax are different.

Aggregation in MongoDB

The basic aggregation API looks like this:

db.users.aggregate()

So, the operation that we want to perform are being inserted into aggregate().

Some of the common aggregate() method comparison between MongoDB and SQL

MongoDB SQL Name
$sum SUM() Get the Sum
$avg AVG() Get the Average
$min MIN() Get the Minimum value
$max MAX() Get the Maximum value
$first TOP/LIMIT Get the first value
$last TOP/LIMIT with ID desc Get the last value
$group GROUP BY Grouping of data
distinct() DISTINCT() Find distinct data

Example 1: Find total posts by each author with $group and $sum

In this example, I am showcasing how to query out total post by each author.

Create the database

I create 3 insert query to save the data.

db.users.insert({
	title: 'Blog post #1',
	body: 'Lorem ipsum dolor sit amet, consectetur.',
	author: 'superoo7',
	url: 'post/post#1',
	tags: ['post', 'blog'],
	upvotes: 10
	});
db.users.insert({
	title: 'Blog post #2',
	body: 'Lorem ipsum dolor sit amet, consectetur.',
	author: 'johnson',
	url: 'blog/post#2',
	tags: ['database', 'blog'],
	upvotes: 109
	});	
db.users.insert({
	title: 'Blog post #3',
	body: 'Lorem ipsum dolor sit amet, consectetur.',
	author: 'superoo7',
	url: 'post/post#3',
	tags: ['life', 'blog'],
	upvotes: 98
	});

To show out all the data, run db.users.find().pretty().

Aggregation with $group and $sum

Run the following aggregation:

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

So the first group is 'GROUP BY' in SQL query, and the total_posts is a new variable, where it content $sum: 1 which means that whenever they find an object contain author, they will increment by 1.

The return value is in the format stated in the query: {"_id": "author", "total_posts": 2}

Which is equivalent to following SQL queries
:

SELECT 
  author,
  COUNT(*) AS total_posts
FROM 
  users
GROUP BY
  author

Example 2: Find all tags with distinct()

In this example, the data can be used back in Example 1.

Aggregation with Distinct()

db.users.distinct("tags");

This query will execute and find all distinct tags (non-repeating tags), and show case it out.

Which is equivalent to following SQL queries:

SELECT
  DISTINCT tags
FROM 
  users

To get count of that certain tag, you can use find() and count().

db.users.find({'tags': 'blog'}).count();

In SQL, it looks like this:

SELECT
  COUNT(DISTINCT tags)
FROM 
  users

Example 3: Sorting data with sort()

Using the same data in Example 1, we can sort the data base on upvotes.

Run the following query and the data will be sorted based on upvotes (descending order)

db.users.find().sort({upvotes: -1}).pretty();

sort() is the function for sorting, and in the sort function, we pass in upvotes as the data to sort with the setup of -1 meaning that we want it to be descending. To make it sort in ascending, simply just change -1 to 1.

In SQL wise, it looks like this:

SELECT *
FROM users
SORT BY
	upvotes DESC

Final Thoughts

Although MongoDB is Document based NoSQL, but both SQL and NoSQL shares some common property to do query. In this tutorial, I showcase some similar query can be done in both MongoDB and SQL which is to make those who are familiar to SQL to understand more about how Document-Based NOSQL works.


Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

43

comments