Mongo aggregate notes

Mongo is difficult to use, mainly because it is not used to writing many words

The documentation is very well written.

https://docs.mongodb.com/manual/reference/aggregation-variables/#agg-system-variables

 

 

https://docs.mongodb.com/manual/reference/operator/aggregation/sort/index.html

 

 

 

 

https://docs.mongodb.com/manual/reference/operator/aggregation/group/index.html

 

 

 

 

This article is okay.

https://www.jianshu.com/p/206f036bdfc5

 

Corresponding to mongtemplate in springboot, it is written like this

 

 

 

This is very well written

https://kb.objectrocket.com/mongo-db/mongodb-group-by-multiple-fields-using-aggregation-function-464

 

db.sales.aggregate(
   [
     { $sort: { item: -1, date: -1 } },
     {
       $group:
         {
           _id: "$item",
           firstSalesDate: { $first: "$date" },
           firstId: { $first: "$_id" },
           firstRoot: { $first: "$$ROOT" },
         }
     }
   ]
)

  

Criteria criteria = Criteria.where("xxx").is("xxxx").and("xxx").is(xxx);        
Aggregation aggregation = Aggregation.newAggregation( Aggregation.match(criteria), Aggregation.sort(Sort.Direction.ASC, "createTime"), Aggregation.group("xx", "xx") .first("$id").as("xx") .first("$xxx").as("xx") .first("$xxx").as("xx") .first("$xxxx").as("xx") .first("$xxxx").as("xx") .first("$xxxx").as("xx") );

The more pitfall is that $ _id is called $ id in mongotemplate, less _, and Aggregation.ROOT, Aggregation.CURRENT

 

Guess you like

Origin www.cnblogs.com/tekikesyo/p/12703774.html