如何$匹配特定的键和值列表?

时间:2016-01-19 06:40:17

标签: mongodb aggregation-framework

这是我关于SO的第一个问题,我找不到真正好的答案,但请原谅我,如果这是对另一个问题的重复。

我正在尝试对特定键值对进行聚合,并希望在$ match管道中尽可能地缩小它。

例如,假设$ match管道我想查询以下内容(使用占位符数据,真实数据集不同,键和值):

{"name": "Michael Jordan", "sport": "Basketball"},
{"name": "Barry Bonds", "sport": "Baseball"},
{"name": "Wayne Rooney", "sport": "Soccer"},
{"name": "Deion Sanders", "sport": "Football"},
{"name": "Usain Bolt", "sport": "Track and Field"}

我想查询的内容可能有20-100个。

设计$ match以查询这五个的最佳方式是什么?

我的第一直觉是:

KeywordDailySentiment.aggregate(
       [
           {$match: {
                 $or: [
                    {"name": "Michael Jordan", "sport": "Basketball"},
                    {"name": "Barry Bonds", "sport": "Baseball"},
                    {"name": "Wayne Rooney", "sport": "Soccer"},
                    {"name": "Deion Sanders", "sport": "Football"},
                    {"name": "Usain Bolt", "sport": "Track and Field"}
                 ]
              }
             }
        ]
)

但是正如你所看到的,这看起来很糟糕,而且它不起作用。

为了举例,我将运行像:

这样的聚合
KeywordDailySentiment.aggregate([
                {
                    $match: {
                        /* Insert answer here for help, 
                        currently I am just making two arrays with 
                        all the players and sports and $matching with 
                        a couple of $in statements, probably not 
                        the best way

                        "dimensions.player": {$in: playerArray},
                        "dimensions.sport": {$in: sportArray},

                        */


                        "dimensions.stream": query["dimensions.stream"],
                        "date": {$gte: ago.toDate(), $lt: query.date.toDate()}
                    }
                },
                {
                    $project: {
                        "sentiment": "$dimensions.sentiment",
                        "player": "$dimensions.player",
                        "sport": "$dimensions.sport"
                    }
                },
                {
                    $group: {
                        _id: {
                            sentiment: "$sentiment",
                            player: "$player",
                            sport: "$sport"
                        },
                        value: {$sum: 1}
                    }
                }
    ]
)

0 个答案:

没有答案