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") //
);
--------------------------------------------------------------------------
比较坑的是, id, 少了_, 还有 Aggregation.ROOT, Aggregation.CURRENT
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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 下载的模板, 没有之一