Use aggregate in Mongoose

1. MongoDB aggregation pipeline (Aggregation Pipeline))
The aggregation pipeline can transform and combine the documents in the collection.
Actual items: table related query, data statistics.
MongoDB uses the db.COLLECTION_NAME.aggregate([{<stage>},...]) method
to build and use the aggregation pipeline. First look at the examples given on the official website and feel the usage of the aggregation pipeline.
2. MongoDB Aggregation pipeline operator and expression
Pipeline
operator
Description
$project adds, deletes, and renames the field
$match
conditions match. Only documents that meet the conditions can enter the next
stage.
$limit limits the number of results.
$skip skips the number of documents.
$sort conditional sorting.
$group Conditional combination result statistics
$lookup
$lookup operator is used to introduce data from other collections
(table relational query)
SQL and NOSQL comparison:
pipeline expression:
pipeline operator as the "key", the corresponding "value" is called Pipeline expression.
For example {$match:{status:"A"}}, $match is called a pipeline operator, and status: "A" is called a pipeline expression, which
is the operand of the pipeline operator (Operand).
Each pipeline expression is a document structure, which is composed of field names, field values, and some expression operators.
WHERE $match
GROUP BY $group
HAVING $match
SELECT $project
ORDER BY $sort
LIMIT $limit
SUM() $sum
COUNT() $sum

Guess you like

Origin blog.csdn.net/qq_35622189/article/details/115007585