What is Aggregate in Mongodb?

Asked

Viewed 450 times

3

I know this kind of doubt I find "easily" in the documentation itself and on the internet, however even in the official documentation became very confused and very technical with several unknown terms.

Anyway, I need help understanding the theoretical concept of Aggregate. I’ve researched until I understood a little the functioning, but when they ask me what it is... I have no idea how to answer.

Currently I would answer so:

"Aggregate is a Mongodb method that takes an array of multiple objects. These objects have specific keys that make Aggregate perform actions that operate the data of this schema such as data filter, repeated value counting, summation, grouping, etc."

Only it all seemed lost when a friend of mine said my answer had nothing to do with it. Now it seems that everything I’ve studied and researched makes no sense.

  • 3

    Related: https://answall.com/q/86715/101

  • 2

    This link has nothing to do with the question. The link is about OOP modeling concepts, the aggregation of the question is related to aggregating information and not model, as a group by query SQL, or make a map/reduce of data

  • 3

    If it has nothing to do I would advise people not to use what has a name of something that is not what it says: estou precisando de ajuda para entender o conceito teórico de Aggregate and that has nothing to do with OOP according to the linked answer, is a universal concept of computing.

  • as to the name seems right, is the name of the functionality in the mongodb: https://docs.mongodb.com/manual/aggregation/ as well as on sql-sever: https://docs.microsoft.com/en-us/sql/t-sql/functions/aggregate-functions-transact-sql only the concepts are different, here at the level of information, in OOP, at the level of modeling

  • 2

    "only the concepts are different, here at the level of information, in OOP, at the level of modeling" This is no different concept, it is application of the same concept in different contexts.

  • Sorry @Bacco, even though I won’t be able to convince you, I will defend my point. OOP aggregation is a modeling concept where one object is part of the other. The aggregation in the data, which is what you have in the question, are operations of calculation, grouping and data transformation, which does not require that there are two objects involved, and more, if there are, the objects do not need to be part of each other, that is the difference. A "distinct" is an aggregation operation, which has nothing to do with OOP aggregation, is done in a single column, does not involve 2 objects, understand? So they’re different concepts

Show 1 more comment

2 answers

3


The concepts of aggregation (among other modeling concepts) have in the link @Maniero already commented: What is the difference between Association, Aggregation and Composition in OOP?

The aggregation operations in the mongodb are operations that group the data and return computed results, such as sums and counting for example.

Aggregation Operations process data Records and Return computed Results. Aggregation Operations group values from Multiple Documents Together, and can perform a Variety of Operations on the grouped data to Return a single result.

Of the documentation here: https://docs.mongodb.com/manual/aggregation/

In free translation:

Aggregation operations process data records and return computed results. Aggregation operations group values of various documents and can perform a variety of operations in the grouped data to return a single result.

It is similar to the concept of GROUP BY of a relational database.

In example of aggregation, counting elements would be for example, from such a data source:

[
  {
    "nome": "João",
    "estado": "RJ",
    "pais": "Brasil"
  },
  {
    "nome": "Maria",
    "estado": "RJ",
    "pais": "Brasil"
  },
  {
    "nome": "João",
    "estado": "SP",
    "pais": "Brasil"
  },
  {
    "nome": "Maria",
    "estado": "SP",
    "pais": "Brasil"
  },
  {
    "nome": "João",
    "estado": "PB",
    "pais": "Brasil"
  },
  {
    "nome": "Maria",
    "estado": "PB",
    "pais": "Brasil"
  },
  {
    "nome": "John",
    "estado": "CA",
    "pais": "USA"
  }
]

If you want to aggregate state values and count the number of occurrences in each state, you could write an operation like this:

db.collection.aggregate([
  {
    "$group": {
      _id: "$estado",
      count: {
        $sum: 1
      }
    }
  }
])

That is, a "group" operation, by the "state" property, running "Count", adding 1 for each occurrence. The result of this aggregation operation will be:

[
  {
    "_id": "RJ",
    "count": 2
  },
  {
    "_id": "SP",
    "count": 2
  },
  {
    "_id": "PB",
    "count": 2
  },
  {
    "_id": "CA",
    "count": 1
  }
]

Can be seen running here: https://mongoplayground.net/p/TFE7xvjRGbz

All aggregation operations and commands can be found in the documentation: https://docs.mongodb.com/manual/reference/operator/aggregation/interface/

-3

I’m no expert but I’ve used Agregate to optimize the processing and performance of my application queries.

The Aggregate allows you to pass the parameters you want to yourself, preventing processing from being the burden of your backend application.

Ex: imagine that you want to get from your bank all the orders of a customer.

to solve this problem in the conventional way, you would need to load the customer and also all orders, then process each of them comparing if the customer id is present within the order ref.

with Aggregate, and with the correct formula you can save this processing on the part of your back, and inform to Mongo itself run a $match.

Info

Browser other questions tagged

You are not signed in. Login or sign up in order to post.