MongoDB Aggregation Pipeline & Importing JSON Data

MongoDB provides a powerful way to process and analyze data using the Aggregation Pipeline. In this article, we will learn how to use the aggregation pipeline and also how to import JSON data into a MongoDB database named hck-db.


Importing JSON Data into MongoDB

Before working with aggregation, we need some data in our database. Let's learn how to import JSON data into MongoDB.

Step 1: Prepare Your JSON File

Ensure you have a JSON file with structured data. Example JSON file (students.json):

[
  { "name": "Alice", "age": 22, "score": 85 },
  { "name": "Bob", "age": 24, "score": 78 },
  { "name": "Charlie", "age": 23, "score": 92 }
]

Step 2: Use the mongoimport Command

Use the following command to import the JSON file into MongoDB. Open the terminal and run:

mongoimport --db hck-db --collection students --file students.json --jsonArray

Explanation:

  • --db hck-db → Specifies the database name.
  • --collection students → Creates a collection named students.
  • --file students.json → Specifies the JSON file.
  • --jsonArray → Indicates the file contains a JSON array.

After running this command, the students collection will be created inside hck-db.


Understanding Aggregation Pipeline in MongoDB

Aggregation in MongoDB helps process data in stages, similar to a pipeline. Each stage transforms the data before passing it to the next stage.

Example: Calculating Average Score of Students

Run the following aggregation query in MongoDB:

db.students.aggregate([
  { "$group": { "_id": null, "averageScore": { "$avg": "$score" } } }
]);

Explanation of the Stages:

  1. $group Stage: Groups documents together.
    • "_id": null → No grouping key, meaning all documents are grouped into one.
    • "averageScore": { "$avg": "$score" } → Calculates the average of the score field.

Example Output:

[ { "_id": null, "averageScore": 85 } ]

More Aggregation Examples

1. Filtering Students with Score Above 80

db.students.aggregate([
  { "$match": { "score": { "$gt": 80 } } }
]);

Explanation:

  • $match: Filters documents where score is greater than 80.

2. Sorting Students by Score in Descending Order

db.students.aggregate([
  { "$sort": { "score": -1 } }
]);

Explanation:

  • $sort: Sorts documents by score in descending order (-1).

3. Counting Total Students

db.students.aggregate([
  { "$count": "totalStudents" }
]);

Explanation:

  • $count: Returns the total number of documents in the collection.

Conclusion

The MongoDB Aggregation Pipeline is a powerful tool for processing and analyzing data. In this guide, we learned how to:

  • Import JSON data into MongoDB.
  • Use aggregation pipeline stages like $match, $group, $sort, and $count.