Count events and insert string literal during aggregation

前端 未结 2 1962
滥情空心
滥情空心 2021-01-23 14:39

I have large collection of documents which represent some kind of events. Collection contains events for different userId.

{
    \"_id\" : ObjectId(\"57fd7d00e4b         


        
2条回答
  •  日久生厌
    2021-01-23 15:31

    Check the following query

    db.sandbox.aggregate([{ 
        $group: {
            _id: {
                userId: "$userId", 
                date: {
                    $dateToString: { format: "%Y-%m-%d", date: "$timestamp" }} 
                }, 
            send_message: { 
                $sum: { 
                    $cond: { if: { $eq: ["$event_type", "send_message"] }, then: 1, else: 0 } } 
                },
            clicked_cancel: { 
                $sum: { 
                    $cond: { if: { $eq: ["$event_type", "clicked_cancel"] }, then: 1, else: 0 } 
                } 
            },
            clicked_ok: { 
                $sum: { 
                    $cond: { if: { $eq: ["$event_type", "clicked_ok"] }, then: 1, else: 0 } 
                } 
            }
        }
    }])
    

提交回复
热议问题