繁体   English   中英

Mongodb 聚合不同,平均和总和与两个集合之间的分组

[英]Mongodb aggregation distinct, average and sum with group by between two collection

我有这样的数据的用户集合

{
    "_id" : ObjectId("5da594c15324fec81d000027"),
    "password" : "******",
    "activation" : "Active",
    "userType" : "Author",
    "email" : "something@gmail.com",
    "name" : "Something",
    "profilePicture" : "profile_pictures/5da594c15324fec81d0000271607094354423image.png",
    "__v" : 0
}

另一方面,用户日志有这样的数据

{
    "_id" : ObjectId("5fcb7bb4485c34a41900002b"),
    "duration" : 2.54,
    "page" : 1,
    "activityDetails" : "Viewed Page for seconds",
    "contentType" : "article",
    "activityType" : "articlePageStayTime",
    "label" : 3,
    "bookId" : ObjectId("5f93e2cc74153f8c1800003f"),
    "ipAddress" : "::1",
    "creator" : ObjectId("5da594c15324fec81d000027"),
    "created" : ISODate("2020-12-05T12:23:16.867Z"),
    "__v" : 0
}

我想做的相当于这个 sql 查询

SELECT name,count(page),sum(duration),avg(DISTINCT(label)),COUNT(DISTINCT(bookId)) FROM users JOIN userlogs ON users._id=userlogs.creator where userlogs.activityType<>"articleListeningTime" group by users._id.

我可以进行正常group by sum汇总。但是如何用这个来做avg distinctcount distinct 我正在使用 mongodb 版本 3.2

我认为这不需要$group阶段,您可以使用$setUnion$size$avg运算符,

  • $lookupuserlogs集合
  • $project显示必填字段,并根据您的条件过滤userlogs
  • $projectuserlogs中获取统计信息
    • 使用$size获取总日志数
    • 使用$sum获取总持续时间总和
    • 使用$setUnion$avg获取唯一 label 的平均值
    • 使用$serUnion$size获取唯一bookId的计数
db.users.aggregate([
  {
    $lookup: {
      from: "userlogs",
      localField: "_id",
      foreignField: "creator",
      as: "userlogs"
    }
  },
  {
    $project: {
      name: 1,
      userlogs: {
        $filter: {
          input: "$userlogs",
          as: "u",
          cond: { $ne: ["$$u.activityType", "articleListeningTime"] }
        }
      }
    }
  },
  {
    $project: {
      name: 1,
      totalCount: { $size: "$userlogs" },
      durationSum: { $sum: "$userlogs.duration" },
      labelAvg: { $avg: { $setUnion: "$userlogs.label" } },
      bookIdCount: { $size: { $setUnion: "$userlogs.bookId" } }
    }
  }
])

操场

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM