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

Mongodb I$如何匹配键和值的特定列表?,mongodb,aggregation-framework,Mongodb,Aggregation Framework,这是我关于SO的第一个问题,我找不到一个真正好的答案,但如果这是对另一个问题的重复,请原谅我 我正在尝试对特定的键值对进行聚合,并希望在$match管道中尽可能缩小聚合范围 例如,对于$match管道,我想查询以下内容(使用占位符数据,实际数据集不同,键和值): 可能有20-100件我想查询的具体事情 为这五个查询设计$match的最佳方法是什么? 我的第一反应是: KeywordDailySentiment.aggregate( [ {$match: {

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

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

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

可能有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}
                    }
                }
    ]
)

查询是否应该匹配
{“name”:“Barry Bonds”,“sport”:“Soccer}
?不,只匹配“barball”,只匹配键和值。查询是否应该匹配
{“name”:“Barry Bonds”,“sport”:“Soccer}
?不,只匹配“barball”,只匹配键和值。
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}
                    }
                }
    ]
)