Mongo聚合笔记

mongo很难用, 主要是因为不习惯写很多字

文档写得很不错.

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

 

 

 

 

这篇写得也还可以.

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

 

对应springboot 中mongtemplate, 就是这样写

 

 

 

这个写得很不错

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") );

stackflow上的一段
-------------------------------------
-------------------------------------

Aggregation agg = newAggregation( //
match(where("hotelCode").is("0360")), //
sort(Direction.DESC, "confirmationNumber", "timestamp"), //
group("confirmationNumber") //
.first("timestamp").as("timestamp") //
.first(Aggregation.ROOT).as("reservationImage") //
);

--------------------------------------------------------------------------

 



复制代码

比较坑的是, idmongotemplateid, 少了_, 还有 Aggregation.ROOT, Aggregation.CURRENT

 

posted @   multitude  阅读(276)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
历史上的今天:
2019-04-15 coding规约的网站, 从sonar中链接过去
2019-04-15 下载的模板, 没有之一
点击右上角即可分享
微信分享提示