Aggregation en ;MongoDB
Introducción a Agregaciones en MongoDB
Fuente:
https://docs.mongodb.com/manual/aggregation/
Las operaciones de agregación procesan los registros de datos y devuelven los resultados calculados. Las operaciones de agregación agrupan los valores de varios documentos juntos y pueden realizar una variedad de operaciones en los datos agrupados para devolver un solo resultado. MongoDB proporciona tres formas de realizar la agregación: el canal de agregación, la función de reducción de mapas y los métodos de agregación de propósito único.
Aggregation Pipeline
Map-Reduce
Single Purpose Aggregation Operations
SQL vs Aggregation
Fuente MongoDB ejemplos
https://docs.mongodb.com/manual/reference/sql-aggregation-comparison/
SQL Terms, Functions, and Concepts | MongoDB Aggregation Operators |
WHERE | |
GROUP BY | |
HAVING | |
SELECT | |
ORDER BY | |
LIMIT | |
SUM() | |
COUNT() | |
join |
Ejemplos:
SQL | Description |
SELECT COUNT(*) AS count FROM orders | Count all records from orders |
SELECT SUM(price) AS total FROM orders | Sum the price field from orders |
SELECT cust_id, SUM(price) AS total FROM orders GROUP BY cust_id | For each unique cust_id, sum the price field. |
SELECT cust_id, SUM(price) AS total FROM orders GROUP BY cust_id ORDER BY total | For each unique cust_id, sum the price field, results sorted by sum. |
SELECT cust_id, ord_date, SUM(price) AS total FROM orders GROUP BY cust_id, ord_date | For each unique cust_id, ord_date grouping, sum the price field. Excludes the time portion of the date. |
SELECT cust_id, count(*) FROM orders GROUP BY cust_id HAVING count(*) > 1 | For cust_id with multiple records, return the cust_id and the corresponding record count. |
SELECT cust_id, ord_date,SUM(price) AS total FROM orders GROUP BY cust_id, ord_date HAVING total > 250 | For each unique cust_id, ord_date grouping, sum the price field and return only where the sum is greater than 250. Excludes the time portion of the date. |
SELECT cust_id, SUM(price) as total FROM ordersWHERE status = 'A' GROUP BY cust_id | For each unique cust_id with status A, sum the price field. |
SELECT cust_id, SUM(price) as total FROM orders WHERE status = 'A' GROUP BY cust_id HAVING total > 250 | For each unique cust_id with status A, sum the price field and return only where the sum is greater than 250 |
SELECT cust_id, SUM(li.qty) as qty FROM orders o, order_lineitem li WHERE li.order_id = o.id GROUP BY cust_id | For each unique cust_id, sum the corresponding line item qty fields associated with the orders. |
SELECT COUNT(*) FROM (SELECT cust_id, ord_date FROM orders GROUP BY cust_id, ord_date) as DerivedTable | Count the number of distinct cust_id, ord_date groupings. Excludes the time portion of the date. |
SQL Example
Fuente:
MongoDB Aggregation Example
https://examples.javacodegeeks.com/software-development/mongodb/mongodb-aggregation-example/
$project
$match
$limit
$group
$sort
$lookup
$out
$unwind
Expresiones usadas en la funciòn Aggregation
Expression | Description |
| Suma |
| Promedio |
| Retorna el valor mínimo |
| Retorna el valor máximo. |
AGGREGATION BUILDER
Fuente:
http://mongodb.github.io/mongo-java-driver/3.2/builders/aggregation/
Se pueden mezclar con Document().
Match
The$match
pipeline stage passes all documents matching the specified filter to the next stage. Though the filter can be an instance of any class that implementsBson
, it’s convenient to combine with use of theFilters
class. In the example below, it’s assumed that theeq
method of theFilters
class has been statically imported.
This example creates a pipeline stage that matches all documents where theauthor
field is equal to"Dave"
:
Project
The$project
pipeline stage passes the projected fields of all documents to the next stage. Though the projection can be an instance of any class that implementsBson
, it’s convenient to combine with use of theProjections
class. In the example below, it’s assumed that theinclude
,excludeId
, andfields
methods of theProjections
class have been statically imported.
This example creates a pipeline stage that excludes the_id
field but includes thetitle
andauthor
fields:
Projecting Computed Fields
The$project
stage can project computed fields as well.
This example simply projects theqty
field into a new field calledquantity
. In other words, it renames the field:
Sample
The$sample
pipeline stage randomly select N documents from its input. This example creates a pipeline stage that randomly selects 5 documents from the collection:
Sort
The$sort
pipeline stage passes all documents to the next stage, sorted by the specified sort criteria. Though the sort criteria can be an instance of any class that implementsBson
, it’s convenient to combine with use of theSorts
class. In the example below, it’s assumed that thedescending
,ascending
, andorderBy
methods of theSorts
class have been statically imported.
This example creates a pipeline stage that sorts in descending order according to the value of theage
field and then in ascending order according to the value of theposts
field:
Skip
The$skip
pipeline stage skips over the specified number of documents that pass into the stage and passes the remaining documents to the next stage.
This example skips the first5
documents:
Limit
The$limit
pipeline stage limits the number of documents passed to the next stage.
This example limits the number of documents to10
:
Lookup
Starting in 3.2, MongoDB provides a new$lookup
pipeline stage that performs a left outer join with another collection to filter in documents from the joined collection for processing.
This example performs a left outer join on thefromCollection
collection, joining thelocal
field to thefrom
field and outputted in thejoinedOutput
field:
Group
The$group
pipeline stage groups documents by some specified expression and outputs to the next stage a document for each distinct grouping. A group consists of an_id
which specifies the expression on which to group, and zero or moreaccumulatorswhich are evaluated for each grouping. To simplify the expression of accumulators, the driver includes anAccumulators
class with static factory methods for each of the supported accumulators. In the example below, it’s assumed that thesum
andavg
methods of theAccumulators
class have been statically imported.
This example groups documents by the value of thecustomerId
field, and for each group accumulates the sum and average of the values of thequantity
field into thetotalQuantity
andaverageQuantity
fields, respectively.
Unwind
The$unwind
pipeline stage deconstructs an array field from the input documents to output a document for each element.
This example outputs, for each document, a document for each element in thesizes
array:
Available with MongoDB 3.2, this example also includes any documents that have missing ornull
values for the$sizes
field or where the$sizes
list is empty:
Available with MongoDB 3.2, this example unwinds thesizes
array and also outputs the array index into the$position
field:
Out
The$out
pipeline stage outputs all documents to the specified collection. It must be the last stage in any aggregate pipeline:
This example writes the pipeline to theauthors
collection:
Creating a Pipeline
The above pipeline operators are typically combined into a list and passed to theaggregate
method of aMongoCollection
. For instance:
Last updated