Aggregation en ;MongoDB
Last updated
Was this helpful?
Last updated
Was this helpful?
Fuente:
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 Terms, Functions, and Concepts
MongoDB Aggregation Operators
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
LIMIT
SUM()
COUNT()
join
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:
$project
$match
$limit
$group
$sort
$lookup
$out
$unwind
Expresiones usadas en la funciòn Aggregation
Expression
Description
$sum
Suma
$avg
Promedio
$min
Retorna el valor mínimo
$max
Retorna el valor máximo.
Fuente:
Se pueden mezclar con Document().
This example creates a pipeline stage that matches all documents where theauthor
field is equal to"Dave"
:
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:
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:
This example skips the first5
documents:
This example limits the number of documents to10
:
This example performs a left outer join on thefromCollection
collection, joining thelocal
field to thefrom
field and outputted in thejoinedOutput
field:
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.
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:
This example writes the pipeline to theauthors
collection:
The above pipeline operators are typically combined into a list and passed to theaggregate
method of aMongoCollection
. For instance:
Thepipeline 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 theclass. In the example below, it’s assumed that theeq
method of theFilters
class has been statically imported.
Thepipeline 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 theclass. In the example below, it’s assumed that theinclude
,excludeId
, andfields
methods of theProjections
class have been statically imported.
Thepipeline stage randomly select N documents from its input. This example creates a pipeline stage that randomly selects 5 documents from the collection:
Thepipeline 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 theclass. In the example below, it’s assumed that thedescending
,ascending
, andorderBy
methods of theSorts
class have been statically imported.
Thepipeline stage skips over the specified number of documents that pass into the stage and passes the remaining documents to the next stage.
Thepipeline stage limits the number of documents passed to the next stage.
Starting in 3.2, MongoDB provides a newpipeline stage that performs a left outer join with another collection to filter in documents from the joined collection for processing.
Thepipeline 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 morewhich are evaluated for each grouping. To simplify the expression of accumulators, the driver includes anclass 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.
Thepipeline stage deconstructs an array field from the input documents to output a document for each element.
Thepipeline stage outputs all documents to the specified collection. It must be the last stage in any aggregate pipeline: