Skip to content

ProgrammingHero1/bistro-boss-server-with-aggregate-part_8

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What We Did Today

  • Different Dashboard Home Page for User and Admin: Implemented distinct home pages for users and administrators to provide customized experiences based on roles.
  • Fix Login Issue and API Race Condition: Resolved issues related to user login and addressed race conditions in API requests to ensure smooth and reliable operations.
  • Create Admin Dashboard Stats API: Developed an API to fetch and display statistical data for the admin dashboard, providing insights into various metrics.
  • Load Data and Display Stats on the Admin Home: Loaded statistical data and displayed it on the admin home page to give administrators a quick overview of important metrics.
  • High-Level Overview of Aggregate Pipeline: Provided a high-level overview of the MongoDB aggregate pipeline for data aggregation and analysis.
  • Get Order Quantity and Revenue by Category: Used aggregate functions to calculate order quantities and revenue by category, enabling detailed financial insights.
  • Display Custom Bar Chart and Custom Pie Chart: Created custom bar and pie charts to visually represent data, making it easier to understand and analyze.
  • Deploy Bistro Boss to Vercel and Firebase: Deployed the Bistro Boss application to Vercel and Firebase for hosting, ensuring scalability and high availability.

MongoDB Aggregation Documentation

Overview

MongoDB's aggregation framework is a powerful tool for data processing and analysis, allowing you to transform and summarize data in a variety of ways. Aggregations are operations that process data records and return computed results. They are often used to group values from multiple documents together, perform operations on the grouped data, and return a single result.

Aggregation Pipeline

The aggregation pipeline is a framework for data aggregation, modeled on the concept of data processing pipelines. Documents enter a multi-stage pipeline that transforms the documents into an aggregated result. Each stage in the pipeline performs an operation on the input documents and passes the results to the next stage.

Basic Syntax

db.collection.aggregate([
  { $stage1: { /* stage1 options */ }},
  { $stage2: { /* stage2 options */ }},
  // More stages as needed
])

MongoDB Aggregation Pipeline Guide

Common Aggregation Stages

$match

Filters documents based on specified conditions.

{ $match: { status: "A" } }

$group

Groups documents by a specified identifier and applies accumulator expressions to each group.

{ 
  $group: {
    _id: "$field",
    total: { $sum: "$amount" }
  }
}

$project

The $project stage reshapes documents by adding or removing fields. It allows you to specify which fields to include or exclude in the output documents, along with computed fields using expressions.

Example:

{ 
  $project: {
    name: 1,
    total: { $sum: "$items.price" }
  }
}

$sort

The $sort stage sorts input documents based on the specified criteria. It's useful for ordering documents based on one or more fields, either ascending or descending.

Example:

{ $sort: { total: -1 } }

$limit

Limits the number of documents passed to the next stage.

{ $limit: 5 }

$lookup

Performs a left outer join with another collection.

{
  $lookup: {
    from: "otherCollection",
    localField: "localField",
    foreignField: "foreignField",
    as: "fromItems"
  }
}

Example Aggregation Pipeline: Calculate Total Sales by Category

db.sales.aggregate([
  { 
    $match: { 
      date: { $gte: ISODate("2024-01-01"), $lt: ISODate("2025-01-01") }
    } 
  },
  { 
    $group: {
      _id: "$category",
      totalSales: { $sum: "$amount" }
    } 
  },
  { 
    $sort: { totalSales: -1 } 
  }
])

$unwind

Deconstructs an array field into separate documents.

{ $unwind: "$items" }

$addFields

Adds new fields to documents.

{ 
  $addFields: {
    totalAfterDiscount: { $subtract: ["$total", "$discount"] }
  } 
}

This example calculates a new field totalAfterDiscount by subtracting the value of the field discount from the value of the field total.

$redact

Controls document field visibility.

{
  $redact: {
    $cond: {
      if: { $eq: ["$status", "A"] },
      then: "$$KEEP",
      else: "$$PRUNE"
    }
  }
}

This example controls the visibility of document fields based on the value of the status field. If the status field is equal to "A", the document is kept ($$KEEP), otherwise it's pruned ($$PRUNE).

$out

Writes aggregation results to a specified collection.

{ $out: "resultCollection" }

This stage saves the results of the aggregation pipeline to a new collection named "resultCollection".

Best Practices

  • Indexing: Ensure indexing on fields used in $match, $group, and $lookup stages for better performance.
  • Pipeline Optimization: Place $match and $sort stages early in the pipeline to reduce document processing.
  • Stage Limits: Be aware of memory limitations; consider breaking complex pipelines into multiple stages or using $merge.

Conclusion

MongoDB's aggregation framework is versatile and powerful, allowing for complex data transformations and analyses. Mastering the various stages and techniques enables efficient data processing and valuable insights extraction.

For detailed information, refer to the official MongoDB documentation.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published