In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.