【问题标题】:Aggregate total and unique counts based on value type and unique visitorId - MongoDB根据值类型和唯一访问者 ID 汇总总计数和唯一计数 - MongoDB
【发布时间】:2022-02-11 20:49:18
【问题描述】:

类似于我的另一个问题 (Here)。但现在我正在尝试根据以下数据形状计算每种事件类型每天的唯一事件和总事件:

{
    username: "jack",
    events: [
     {
       eventType: "party",
       createdAt: "2022-01-23T10:26:11.214Z",
       visitorInfo: {
            visitorId: "87654321-0ebb-4238-8bf7-87654321"
           }
     },
     {
       eventType: "party",
       createdAt: "2022-01-23T10:26:11.214Z",
       visitorInfo: {
            visitorId: "87654321-0ebb-4238-8bf7-87654321"
           }
     },
     {
       eventType: "party",
       createdAt: "2022-01-23T10:26:11.214Z",
       visitorInfo: {
            visitorId: "01234567-0ebb-4238-8bf7-01234567"
           }
     },
    {
       eventType: "meeting",
       createdAt: "2022-01-23T12:26:11.214Z",
       visitorInfo: {
            visitorId: "87654321-0ebb-4238-8bf7-87654321"
           }
     },
     {
       eventType: "meeting",
       createdAt: "2022-01-23T11:26:11.214Z",
       visitorInfo: {
            visitorId: "87654321-0ebb-4238-8bf7-87654321"
           }
     },
     {
       eventType: "meeting",
       createdAt: "2022-01-23T12:26:11.214Z",
       visitorInfo: {
            visitorId: "01234567-0ebb-4238-8bf7-01234567"
           }
     },
     {
       eventType: "party",
       createdAt: "2022-01-30T10:26:11.214Z",
       visitorInfo: {
            visitorId: "12345678-0ebb-4238-8bf7-12345678"
           }
     },
    {
       eventType: "party",
       createdAt: "2022-01-30T10:16:11.214Z",
       visitorInfo: {
            visitorId: "12345678-0ebb-4238-8bf7-12345678"
           }
     },
    {
       eventType: "meeting",
       createdAt: "2022-01-30T12:36:11.224Z",
       visitorInfo: {
            visitorId: "12345678-0ebb-4238-8bf7-12345678"
           }
     },
    {
       eventType: "meeting",
       createdAt: "2022-01-30T11:46:11.314Z",
       visitorInfo: {
            visitorId: "12345678-0ebb-4238-8bf7-12345678"
           }
     }
       ]

    }

我正在尝试计算日期(每天)的事件(基于 visitorId 的所有事件和唯一事件)。 这就是我目前所拥有的(感谢@R2D2 的方法指南):

 Event.aggregate([
  { $match: { username: 'jack' } },

  { $unwind: "$events" },

  {
    $project: {
      totalPartyEvents: {
        $cond: [
          {
            $eq: ["$events.eventType", "party"],
          },
          1,
          0,
        ],
      },

    uniquePartyEvents: { // where I'm stuck. I need to count unique events based on visitorId on current date for 'party' event type.
        $cond: [
          {
            $eq: ["$events.eventType", "party"],
          },
          1,
          0,
        ],
      },

      totalMeetingEvents: {
        $cond: [
          {
            $eq: ["$events.eventType", "meeting"],
          },
          1,
          0,
        ],
      },

    uniqueMeetingEvents: { // do the same for other events. maybe there's a better way to combine these (with facets).
        $cond: [
          {
            $eq: ["$events.eventType", "meeting"],
          },
          1,
          0,
        ],
      },

      date: "$events.createdAt",
    },
  },

  {
    $group: {
      _id: {
        $dateToString: { format: "%Y-%m-%d", date: "$date" },
      },

      totalPartyEvents: {
        $sum: "$totalMeetingEvents",
      },
      uniquePartyEvents: {
        $sum: "$totalMeetingEvents",
      },
    
      totalMeetingEvents: {
        $sum: "$totalMeetingEvents",
      },

      uniqueMeetingEvents: {
        $sum: "$uniqueMeetingEvents",
      },
    },
  },
  {
    $project: {
      date: "$_id",
      uniquePartyEvents: 1,
      totalPartyEvents: 1,
      totalMeetingEvents:1,
      uniqueMeetingEvents: 1,

    },
  },
  {
    $group: {
      _id: "0",
      dateAndEventFrequency: {
        $push: "$$ROOT",
      },
    },
  },
  {
    $project: {
      _id: 0,
      dateAndEventFrequency: 1,
    },
  },
]);

我尝试使用$addToSet,但它不能与$project 一起使用(它可以与$group 一起使用)。 根据数据形状和我期望的预期结果,欢迎任何新方法。我使用了$project,因为我已经在使用它了。

基本上我希望最终得到什么:

dateAndEventFrequency: [
    {
    _id: "2022-01-23",
    totalPartyEvents: 3,
    uniquePartyEvents: 2,
    totalMeetingEvents: 3,
    uniqueMeetingEvents: 2,
    date: "2022-01-23",
    },
   {
    _id: "2022-01-30",
    totalPartyEvents: 2,
    uniquePartyEvents: 1,
    totalMeetingEvents: 2,
    uniqueMeetingEvents: 1,
    date: "2022-01-30",
    },
]

我正在使用 Mongoose 和 Nodejs。任何帮助或指导表示赞赏。谢谢!

【问题讨论】:

    标签: node.js mongodb mongoose nosql aggregate


    【解决方案1】:

    mongo playground

    db.collection.aggregate([
      {
        $match: {
          username: "jack"
        }
      },
      {
        "$unwind": "$events"
      },
      {
        "$match": {
          "events.eventType": {
            "$in": [
              "meeting",
              "party"
            ]
          }
        }
      },
      {
        "$group": {
          "_id": {
            date: {
              "$dateToString": {
                format: "%Y-%m-%d",
                date: "$events.createdAt"
              }
            },
            "visitorId": "$events.visitorInfo.visitorId",
            "eventType": "$events.eventType"
          },
          "count": {
            "$sum": 1
          }
        }
      },
      {
        "$group": {
          "_id": {
            "date": "$_id.date",
            "eventType": "$_id.eventType"
          },
          "uniqueTotal": {
            "$sum": 1
          },
          total: {
            "$sum": "$count"
          }
        }
      },
      {
        "$group": {
          "_id": "$_id.date",
          "partyUniqueTotal": {
            "$sum": {
              "$cond": [
                {
                  $eq: [
                    "$_id.eventType",
                    "party"
                  ],
                  
                },
                "$uniqueTotal",
                0
              ]
            }
          },
          "totalPartyEvents": {
            "$sum": {
              "$cond": [
                {
                  $eq: [
                    "$_id.eventType",
                    "party"
                  ],
                  
                },
                "$total",
                0
              ]
            }
          },
          "meetingUniqueTotal": {
            "$sum": {
              "$cond": [
                {
                  $eq: [
                    "$_id.eventType",
                    "meeting"
                  ],
                  
                },
                "$uniqueTotal",
                0
              ]
            }
          },
          "totalmeetingEvents": {
            "$sum": {
              "$cond": [
                {
                  $eq: [
                    "$_id.eventType",
                    "meeting"
                  ],
                  
                },
                "$total",
                0
              ]
            }
          }
        }
      }
    ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 1970-01-01
      • 2011-06-09
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多