mongodb进阶聚合查询各种写法

mongodb聚合sql写法

1、聚合查询配上分页加排序

bash 复制代码
DBQuery.shellBatchSize = 30000
db.getCollection('svOrderRecordMo').aggregate([
{$match:{"actionType":9}},
{$group:{_id:"$svOrderId",total:{"$sum":1}}},
{$sort:{"createTime":1}},
{$skip:0},
{$limit:100}
])

2、在1的聚合查询基础上进行match过滤(过滤出total>1的数据)

bash 复制代码
DBQuery.shellBatchSize = 30000
db.getCollection('svOrderRecordMo').aggregate([
{$match:{"actionType":9}},
{$group:{_id:"$svOrderId",total:{"$sum":1}}},
{$match:{"total":{"$gt":1}}},
{$sort:{"createTime":1}},
{$skip:0},
{$limit:3000}
])

3、聚合查询时间范围并把Date的日期格式转成字符串(yyyyMMdd)然后聚合查询

bash 复制代码
db.getCollection('svRegisterRecordMo').aggregate([
  {$match:{"$and":[{"createTime":{"$gte":ISODate("2023-01-01T00:00:00.000+08:00")}}
    ,{"createTime":{"$lte":ISODate("2023-07-02T00:00:00.000+08:00")}}]   }},

  {
    $project: {
      convertedDate: {
        $dateToString: {
          format: "%Y%m%d",
          date: "$createTime"
        }
      }
    }
  },

  {
    $group: {
      _id: "$convertedDate",
      count: { $sum: 1 }
    }
  }
])

4、排序,聚合,sum求和同时应用

bash 复制代码
db.getCollection('reportAgentMo').aggregate([
{$match:{"dataType":"1.1","reportType":1,"platformType":2,"reportDate":{"$in":["2023-01-25","2023-02-25","2023-03-25","2023-04-25","2023-05-25","2023-06-25","2023-07-25","2023-08-25"]}}},
{$group:{_id:"$agentId",total:{"$sum":"$issuePercent"}}},
{$sort:{"total":-1}},
{$limit:10}
])

5、聚合对两字段求和,并把两个求和的值相除然后排序

bash 复制代码
db.getCollection('reportAgentMo').aggregate([
{$match:{"dataType":"1.1","reportType":1,"platformType":2,
    "reportDate":{"$in":["2023-01-25","2023-02-25","2023-03-25","2023-04-25","2023-05-25","2023-06-25","2023-07-25","2023-08-25"]}}},
{$group:{_id:"$agentId",total1:{"$sum":"$issueNum"},total2:{"$sum":"$svNum"}}},
{$match:{total2:{"$gt":0}}},
{$project:{_id:1,aver:{"$divide":["$total1","$total2"]}}},
{$sort:{aver:-1}},
{$limit:11}
])

6、聚合时使用$first关键字(根据customTextAnswer.text聚合,并获取第一条数据)

bash 复制代码
db.getCollection('orderExtraInfoMo').aggregate([
{$match:{"topic":"家庭年收入"
    ,"customTextAnswer.number":{"$ne":null}}},
{$group:{_id:"$customTextAnswer.text",svOrderId:{"$first":"$svOrderId"},"number":{"$first":"$customTextAnswer.number"}}},
{$sort:{"svOrderId":-1}},
{$skip:0},
{$limit:10000}
])

7、对targetValue字段进行聚合统计,key是cId

bash 复制代码
db.getCollection("channelTargetMo").aggregate([
{$match:{"targetDate":{"$gte":"2022-08-03"},"targetDate":{"$lte":"2022-09-02"},"targetDateType":"day","targetType":"deal_sv","cIdType":"subId","cId":"62eb25c3b6d6033cb3fd9174"}},
{$group:{_id:"$cId",total:{"$sum":"$targetValue"}}}
]);

8、mongodb的两表关联查询

如下所示:左表是reportCustDealInfoMo,右表是reportCustDataMo,连接条件是reportCustDealInfoMo.custId=reportCustDataMo.cId。因为默认是1对多 情况,所以aa是一个数组,

$unwind 是把aa数组展开

bash 复制代码
db.reportCustDealInfoMo.aggregate([
{
$match:{"isDeleted":false,"isDeal":true}
},

  {
    $lookup: {
      from: "reportCustDataMo",
      localField: "custId",
      foreignField: "cId",
      as: "aa"
    }
  },
  {
    $unwind: "$aa" 
  },

    {
    $match: {
      "aa.cIdType": "custId",
      "aa.reportType": "year",
      "aa.reportDate":"2021"
    }
  },
      {$group:{_id:null,
        
        dealInternetAmount:{"$sum":"$aa.dealInternetAmount"},
dealHighCriterionAmount:{"$sum":"$aa.dealHighCriterionAmount"},
dealCarCriterionAmount:{"$sum":"$aa.dealCarCriterionAmount"},
dealGroupCriterionAmount:{"$sum":"$aa.dealGroupCriterionAmount"},
dealLifeCriterionAmount:{"$sum":"$aa.dealLifeCriterionAmount"},
underwriteCdCriterionAmount:{"$sum":"$aa.underwriteCdCriterionAmount"}
    }}
    ,
    {$project:{
        dealInternetAmount:1,
        dealHighCriterionAmount:1,
        dealCarCriterionAmount:1,
        dealGroupCriterionAmount:1,
        dealLifeCriterionAmount:1,
	underwriteCdCriterionAmount:1,
        totalStaAmount:{"$add":["$dealInternetAmount","$dealHighCriterionAmount","$dealCarCriterionAmount","$dealGroupCriterionAmount","$dealLifeCriterionAmount"]}}}
])
相关推荐
敲上瘾3 分钟前
Docker 存储卷(Volume)核心概念、类型与操作指南
linux·服务器·数据库·docker·容器·架构
DemonAvenger12 分钟前
MySQL内存优化:缓冲池与查询缓存调优实战指南
数据库·mysql·性能优化
RationalDysaniaer16 分钟前
了解etcd
数据库·etcd
正在走向自律29 分钟前
国产时序数据库选型指南-从大数据视角看透的价值
大数据·数据库·清华大学·时序数据库·iotdb·国产数据库
Pocker_Spades_A30 分钟前
Python快速入门专业版(十五):数据类型实战:用户信息录入程序(整合变量、输入与类型转换)
数据库·python
禁默44 分钟前
已知 inode 号,如何操作文件?Ext 文件系统增删查改底层逻辑拆解
linux·服务器·数据库
云飞云共享云桌面1 小时前
工厂办公环境如何实现一台服务器多人共享办公
运维·服务器·网络·数据库·3d
weixin_456904271 小时前
MySQL高级特性详解
数据库·mysql
Elastic 中国社区官方博客1 小时前
使用 cloud-native Elasticsearch 与 ECK 运行
大数据·数据库·elasticsearch·搜索引擎·kubernetes·k8s·全文检索
程序员爱钓鱼1 小时前
Go语言实战案例 — 工具开发篇:编写高可用日志收集脚本
后端·mongodb·go