📔
Databases
  • Table of contents
  • SQL
    • Getting Started
      • PgAdmin Tool
    • Data Types
      • Arrays
      • Date/Time/Stamps
      • JSON
      • Internal Functions
      • Sequences
      • User Defined Data Types
    • Database
      • Schema
    • Table
      • Aggregation
      • Usefull Functions
      • Combining Tables
      • Constraints
      • Common Table Expression
      • GROUP BY and HAVING
      • OPERATORS
      • ORDER BY and DISTINCT
      • Views
      • WHERE Clause
    • Order of SQL Execution
    • Advance Table
      • Internals
      • Managing Tables
      • Partitioning Tables
      • Pivotal or Crosstab Tables
    • Joins
      • Cross & Natural Joins
      • Full, Multiple & Self Joins
      • Inner Join
      • Left and Right JOIN
    • Functions
      • Cursors
      • PL/pgSQL
      • Stored Procedures
      • Triggers
        • More on Triggers
    • Indexing
      • Custom Indexes
      • GIN Index
      • Indexes
      • SQL
      • Unique Index
    • Summarization
      • SubQueries
      • Window
  • MongoDB
    • Mongo Administration
    • MongoDB Aggregation
    • MQL
  • Redis
  • Cassandra
    • CQL
    • Data Modelling
      • Advance Data Modelling
    • Cassandra Administration
    • Cassandra Features
Powered by GitBook
On this page

Was this helpful?

  1. MongoDB

MongoDB Aggregation

Aggregate Framework

  • Queries are written inside [] operator, denoting the order in which hey execute

  • $group : An operator that takes in multiple streams of data and distributes it into multiple reservoirs

# MQL Query
db.listingsAndReviews.find({ "amenities": "Wifi" },
                           { "price": 1, "address": 1, "_id": 0 }).pretty()

# MQL Query ith aggregation framework
db.listingsAndReviews.aggregate(
    [
      { "$match": { "amenities": "Wifi" } },
      { "$project": { "price": 1,
                      "address": 1,
                      "_id": 0 }}
    ]).pretty()
# Find one document in the collection 
# and only include the address field in the resulting cursor.
db.listingsAndReviews.findOne({ },{ "address": 1, "_id": 0 })

# Project only the address field value for each document, 
# then group all documents into one document per address.country value.
db.listingsAndReviews.aggregate(
    [   { "$project": { "address": 1, "_id": 0 }},
        { "$group": { "_id": "$address.country" }}
    ])

# Project only the address field value for each document, 
# then group all documents into one document per address.country value, 
# and count one for each document in each group.
db.listingsAndReviews.aggregate(
    [
          { "$project": { "address": 1, "_id": 0 }},
          { "$group": { "_id": "$address.country",
                        "count": { "$sum": 1 } } }
    ])
PreviousMongo AdministrationNextMQL

Last updated 3 years ago

Was this helpful?