通过MongoDB聚合获取组中的百分比

时间:2019-05-21 07:45:47

标签: mongodb mongoose mongodb-query aggregation-framework

我和猫鼬疯狂地聚集在一起。我正在对字段进行分组并成功获取计数。我需要产生100分的分数,但似乎无法使它起作用。我的查询产生一个空的%。

查询

const pipeline = await Company.aggregate([
    {
        $match: {
            $or: [
                {
                    $text: {
                        $search: data.name ? data.name : '',
                    },
                },
                {
                    categories: {
                        $in: data.categories
                            ? data.categories.split(',')
                            : [],
                    },
                },
            ],
        },
    },
    {
        $lookup: {
            from: 'companies',
            localField: '_id',
            foreignField: '_id',
            as: 'company',
        },
    },
    {
        $unwind: '$company',
    },
    {
        $lookup: {
            from: 'ratings',
            localField: '_id',
            foreignField: 'company',
            as: 'rating',
        },
    },
    {
        $unwind: '$rating',
    },
    {
        $project: {
            'company._id': 1,
            'company.name': 1,
            'company.url': 1,
            'company.updatedAt': 1,
            'rating._id': 1,
            'rating.comment': 1,
            'rating.rating': 1,
            'rating.sentiment': 1,
            'rating.createdAt': 1,
        },
    },
    {
        $group: {
            _id: '$company._id',
            count: {
                $sum: 1,
            },
            company: {
                $first: {
                    _id: '$company._id',
                    name: '$company.name',
                    url: '$company.url',
                    logo: '$company.logo',
                    created: '$company.createdAt',
                    updated: '$company.updatedAt',
                },
            },
            score: {
                $first: {
                    $divide: [
                        {
                            $trunc: {
                                $multiply: [
                                    { $avg: '$rating.sentiment' },
                                    100,
                                ],
                            },
                        },
                        100,
                    ],
                },
            },
            percentage: {
                $first: {
                    $concat: [
                        {
                            $substr: [
                                {
                                    $multiply: [
                                        {
                                            $divide: [
                                                '$count',
                                                {
                                                    $literal: '$score',
                                                },
                                            ],
                                        },
                                        100,
                                    ],
                                },
                                0,
                                2,
                            ],
                        },
                        '',
                        '%',
                    ],
                },
            },
        },
    },
    {
        $sort: {
            score: -1,
        },
    },
]);

结果

[
    {
        "_id": "5ce35b0dd677c4bd8215b383",
        "count": 3,
        "company": {
            "_id": "5ce35b0dd677c4bd8215b383",
            "name": "Pusher",
            "url": "https://pusher.com",
            "updated": "2019-05-21T01:57:33.618Z"
        },
        "score": -1.5,
        "percentage": "%"
    },
    {
        "_id": "5ce35af1d677c4bd8215b381",
        "count": 1,
        "company": {
            "_id": "5ce35af1d677c4bd8215b381",
            "name": "Stream",
            "url": "https://getstream.io",
            "updated": "2019-05-21T01:57:05.254Z"
        },
        "score": -2,
        "percentage": "%"
    }
]

请注意,百分比为“%”。有人有想法吗?我在这里转圈。 :)提前感谢,感谢您提供的冗长的汇总代码。

0 个答案:

没有答案