【问题标题】:How to get individual counts of columns in a collection using mongodb query?如何使用 mongodb 查询获取集合中列的单个计数?
【发布时间】:2016-12-15 15:42:34
【问题描述】:

我是 MongoDB 的初学者。我需要从一个 MongoDB 查询中获取各个列的计数。

例如,我有一个集合hotelCollection,其中包含字段cityid , starRating, area, locality。现在我将参数传递给cityid 作为123 到查询,我需要获取结果为:

result: {
    starCount: {
        1star: 5,
        2star: 6,
        3star: 5
    },
    areaCount: {
        area1: 4,
        area2: 12
    },
    localityCount: {
        locality1: 10,
        locality2: 6
    }
}

我尝试使用aggregate 查询,但最后我只得到一列计数。所以我暂时写了 4 个aggregate 查询。现在我面临性能问题。我做了很多谷歌搜索,但找不到任何解决方案。请帮我解决这个问题。

【问题讨论】:

    标签: node.js mongodb mongoose aggregation-framework mongodb-aggregation


    【解决方案1】:

    您需要在 $sum 运算符中使用条件表达式,该表达式将使用比较运算符 $eq 检查cityid , starRating, area, locality 键的值。。 p>

    您可以通过添加此表达式来重组您的管道,如下所示:

    db.hotelCollection.aggregate([
        { "$match": { "cityid": 123 } },
        {
            "$group" : {
                "_id": null, 
                "1star": {
                    "$sum": {
                        "$cond": [
                            { "$eq": ["$starRating", 1]  },
                            1, 0
                        ]
                    }
                },
                "2star": {
                    "$sum": {
                        "$cond": [
                            { "$eq": ["$starRating", 2]  },
                            1, 0
                        ]
                    }
                },
                "3star": {
                    "$sum": {
                        "$cond": [
                            { "$eq": ["$starRating", 3]  },
                            1, 0
                        ]
                    }
                },
                "area1": {
                    "$sum": {
                        "$cond": [
                            { "$eq": ["$area", 1]  },
                            1, 0
                        ]
                    }
                },
                "area2": {
                    "$sum": {
                        "$cond": [
                            { "$eq": ["$area", 2]  },
                            1, 0
                        ]
                    }
                },
                "locality1": {
                    "$sum": {
                        "$cond": [
                            { "$eq": ["$locality", 1]  },
                            1, 0
                        ]
                    }
                },
                "locality2": {
                    "$sum": {
                        "$cond": [
                            { "$eq": ["$locality", 2]  },
                            1, 0
                        ]
                    }
                }
            }
        },
        { 
            "$project" : { 
                "_id": 0,
                "starCount": {
                    "1Star": "$1star",
                    "2Star": "$2star",
                    "3Star": "$3star"
                },
                "areaCount": {
                    "area1": "$area1",
                    "area2": "$area2"
                },
                "localityCount": {
                    "locality1": "$locality1",
                    "locality2": "$locality2"
                }               
            } 
        }
    ])
    

    【讨论】:

    • Chridam:您好,感谢您的更新,抱歉回复晚了。在这里,我可以预测starcount,但对于areas,我无法预测,因为我不知道城市下会有多少区域,而且它是动态内容。请为我提供另一种解决方案来解决我的问题..
    猜你喜欢
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    相关资源
    最近更新 更多