Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.4 kB
1
Indexable
Never
[{
    $match: {
        "date": {
            "$gte": ISODate('2021-09-05T20:39:33.073+00:00')
        }
    }
}, {
    $facet: {
        counts: [{
            $group: {
                _id: {
                    "app": "$appId",
                    "user": "$userId"
                },
                devices: {
                    $addToSet: "$deviceId"
                }
            }
        }, {
            $project: {
                "devices": {
                    $size: "$devices"
                },
            }
        }, {
            $group: {
                _id: {
                    "app": "$_id.app"
                },
                "devicesAvg": {
                    $avg: "$devices"
                },
                "devicesMax": {
                    $max: "$devices"
                },
                "devicesMin": {
                    $min: "$devices"
                },
                "users": {
                    $sum: 1
                }
            }
        }, {
            $project: {
                userDevices: {
                    min: "$devicesMin",
                    max: "$devicesMax",
                    avg: "$devicesAvg"
                },
                users: "$users"
            }
        }, ],
        devices: [{
                $group: {
                    _id: {
                        "app": "$appId",
                        "user": "$userId"
                    },
                    devices: {
                        $addToSet: "$deviceId"
                    },
                }
            },
            {
                $group: {
                    _id: {
                        "app": "$_id.app"
                    },
                    devices: {
                        $addToSet: "$devices"
                    },
                }
            },
            {
                $project: {
                    devices: {
                        $reduce: {
                            input: "$devices",
                            initialValue: [],
                            in: {
                                $concatArrays: ["$$value", "$$this"]
                            },
                        },
                    },
                },
            },
        ],
        endpoints: [{
                $group: {
                    _id: {
                        "app": "$appId",
                        "endpoint": "$endpoint"
                    },
                    count: {
                        $sum: 1
                    }
                }
            },
            {
                $group: {
                    _id: {
                        "app": "$_id.app"
                    },
                    endpoints: {
                        $addToSet: {
                            endpoint: "$_id.endpoint",
                            count: "$count"
                        }
                    },
                }
            },



        ]
    }
}, {
    $project: {
        result: {
            $setUnion: ["$counts", "$devices", "$endpoints"]
        }
    }
}, {
    $unwind: "$result"
}, {
    $replaceRoot: {
        newRoot: "$result"
    }
}, {
    $group: {
        _id: {
            "app": "$_id.app",
        },
        data: {
            $mergeObjects: "$$ROOT"
        }
    }
}, {
    $replaceRoot: {
        newRoot: "$data"
    }
}]