简体   繁体   中英

Trouble converting mongo shell aggregate function to pymongo

I've been struggling with this for a few hours now and I can't find a good solution online.

    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}
]

I've put strings all around the important parts but it just returns TypeError: unhashable type: 'dict' while in mongoShell db it works.

Please help! Or at least give me some comfortable documentation on how to convert it. I couldn't find anything!

我遇到的问题是$ cond标记中缺少“ if”,“ then”和“ else”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM