![](/img/trans.png)
[英]Mongo aggregate query with forEach function in pymongo not working
[英]Trouble converting mongo shell aggregate function to pymongo
我已经为此苦苦挣扎了几个小时,但我在网上找不到一个好的解决方案。
pipeline = [
{"$project":
{
"index": {"$cond": { {"$and":[{ "$gt": [{ "$indexOfArray": ["$index.championId", 17]}, 5]},
{"$lt": [{ "$indexOfArray": ["$index.championId", 22]}, 6]}]}
,{"$slice": ["$playerInfo.info", 0, 5]}, {"$slice": ["$playerInfo.info", 5, 5]}}},
"team_a_champ": { "$slice": ["$playerInfo.info.championId", 0, 5]},
"team_b_champ": { "$slice": ["$playerInfo.info.championId", 5, 5]}
}},
{ "$match": { "$or":[
{ "$and":[{ "team_a_champ" : 17 }, {"team_b_champ":22}, {"index.stats.win":True}]},
{ "$and":[{ "team_a_champ" : 22 }, {"team_b_champ":17}, {"index.stats.win":True}]}]}},
{
"$project":
{
"new_idx": {"$arrayElemAt": ["$index", {"$indexOfArray": ["$index.championId", 17]}]}
}
},
{
"$project":
{
"stats": ["$new_idx.stats.item0", "$new_idx.stats.item1", "$new_idx.stats.item2", "$new_idx.stats.item3",
"$new_idx.stats.item4", "$new_idx.stats.item5"]
}
},
{
"$sort":
{"stats": -1}
},
{"$unwind": "$stats"},
{
"$group":
{
"_id": "$stats",
"count": {"$sum": 1}
}
},
{"$match": {"_id": {"$gt": 0}}},
{"$sort": {"count":-1, "_id": -1}},
{"$limit": 6}
]
我已经在重要部分周围放置了字符串,但它只返回TypeError:不可散列的类型:'dict',而在mongoShell db中则起作用。
请帮忙! 或者至少给我一些舒适的文档,说明如何进行转换。 我什么都找不到!
我遇到的问题是$ cond标记中缺少“ if”,“ then”和“ else”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.