Get the App
SLTechnology News&Howtos  ›  Database  › 

MongoDB aggregate aggregation

Shulou Source: shulou.com Published: 2022-06-01 08:49:18 09月23日 Update

Aggregate is equivalent to group in mysql and a series of operations.

Official website introduction address: http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/

The expression describes the instance $sum summarizes the average .db.mycol.values ([{$group: {_ id: "$by_user", num_tutorial: {$sum: "$likes"}]) $avg calculated from all given values in all document collections ([{$group: {_ id: "$by_user") Num_tutorial: {$avg: "$likes"}}]) $min gets the smallest corresponding value in all files in the collection .db.mycol.collection ([{$group: {_ id: "$by_user", num_tutorial: {$min: "$likes"}}]) $max gets the maximum db.mycol.aggregate of the corresponding value in all files in the collection ([{$group: {_ id: "$by_user") Num_tutorial: {$max: "$likes"}}]) $push value is inserted into an array generated document .db.mycol.document ([{$group: {_ id: "$by_user", url: {$push: "$url"}}]) $addToSet value is inserted into an array to get the resulting document But it does not create a duplicate .db.mycol.document ([{$group: {_ id: "$by_user", url: {$addToSet: "$url"}]) $first gets the first document from the source document according to the grouping. In general, this makes sense, along with some previous applications of "$sort"-stage.db.mycol.aggregate ([{$group: {_ id: "$by_user", first_url: {$first: "$url"}]) $last gets the final document from the source document according to the grouping. Usually, this makes sense, along with some previous applications of "$sort"-stage.db.mycol.aggregate ([{$group: {_ id: "$by_user", last_url: {$last: "$url"}])

The relevant operators is as follows:

$project: used to select specific fields from the collection. (alias, with or without display)

Match: this is a filtering operation, so you can reduce the amount as input to the next stage of the given document. (query conditions)

$group: as discussed above, this is not a practical aggregation. (fields that need to be grouped, sum, etc., are also carried out here)

$sort: file sorting. (sort)

Skip: a given number of documents in the list of files that may be skipped forward. (for paging)

Limit: this limits the number of documents. Take a look at the given number starting from the current location (for paging)

$unwind: this is the array used to close the document. When using an array, the data is a kind of pre-joinded, and once again there are individual files, this operation will be cancelled. Therefore, at this stage, the number of files will increase in the next stage. (this is rarely used.)

The operations in the corresponding java are as follows:

BasicDBObject fields = new BasicDBObject ("email", 1); DBObject project = new BasicDBObject ("$project", fields); BasicDBObject groupFilters = new BasicDBObject ("_ id", "$name"); groupFilters.put ("sumage", new BasicDBObject ("$sum", "$age"); groupFilters.put ("totalage", new BasicDBObject ("$avg", "$age")) BasicDBObject group = new BasicDBObject ("$group", groupFilters); AggregationOutput aggrresult = this.mongoTemplate.getCollection ("test") .requests (matchOpt,project, group); / / if there is any sort,skip, continue to append / / or add all operate to the list, as follows: List list = new ArrayList () List.add (group); list.add (project); AggregationOutput aggrresult = this.mongoTemplate.getCollection ("test") .requests (list); / / this is also possible

Tags: Document file array quantity phase grouping field this is application sort maximum minimum this is not location again alias address such as above instance actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL MariaDB Huawei Shulou Tech Info Docker