如何检查同一文档中 6 个值中的任意 3 个值

How can I check any 3 out of 6 value in same document

我正在 python 制作某种口袋妖怪游戏,我需要聚合方面的帮助,例如,

# I have this at the moment.

# But I know that it isn't correct.
aggregations = [
    {
        "$match": {
            "$and": [
                {"$or": [{'hp': 28}, {'atk': 28}]},
                {"$or": [{'def': 28}, {'spatk': 28}]},
                {"$or": [{'spdef': 28}, {'speed': 28}]}
            ]
        }}]

# Database Structure

# Pokemon, Level, XP, SPDEF, SPATK, SPEED, HP, ATK, DEF.
"""
So I have some flags namely --trip <val>, --quad <val>

So if user do --trip 31, So it should match that in SPDEF, SPATK, SPEED, HP, ATK, DEF (ANY THREE).
"""

使用 $project 如果您的值大于 28,您可以存储在一个新变量中,并且对每个属性都这样做。

然后你用新的$project

总结所有新变量

然后只匹配大于或等于 3 的总数

[
{
    "$project": {
        "has_hp": {
            "$cond": {
                "if": {"$gte": ["$hp", 28]},
                "then": 1,
                "else": 0,
            }
        },
        "has_atk": {
            "$cond": {
                "if": {"$gte": ["$hp", 28]},
                "then": 1,
                "else": 0,
            }
        },
        "has_def": {
            "$cond": {
                "if": {"$gte": ["$hp", 28]},
                "then": 1,
                "else": 0,
            }
        },
        "has_spatk": {
            "$cond": {
                "if": {"$gte": ["$hp", 28]},
                "then": 1,
                "else": 0,
            }
        },
        "has_spdef": {
            "$cond": {
                "if": {"$gte": ["$hp", 28]},
                "then": 1,
                "else": 0,
            }
        },
        "has_speed": {
            "$cond": {
                "if": {"$gte": ["$hp", 28]},
                "then": 1,
                "else": 0,
            }
        }
    }
},
{
    "$project": {
        "total": { "$add": ["$has_hp", "$has_atk", "$has_def", "$has_spatk", "$has_spdef", "$has_speed"]}
    }
},
{
    "$match": {"total": {"$gte": 3}}
}
]