Python 如何检查同一文档中6个值中的任意3个
我正在用python制作一款口袋妖怪游戏,我需要一些聚合方面的帮助,比如Python 如何检查同一文档中6个值中的任意3个,python,database,mongodb,pymongo,Python,Database,Mongodb,Pymongo,我正在用python制作一款口袋妖怪游戏,我需要一些聚合方面的帮助,比如 # I have this at the moment. # But I know that it isn't correct. aggregations = [ { "$match": { "$and": [ {"$or": [{'hp': 28}, {'atk': 28}]},
# 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}]}
]
}}]
#数据库结构
#口袋妖怪,等级,XP,SPDEF,SPATK,速度,HP,ATK,DEF。
"""
所以我有一些旗子,也就是trip,quad
所以,若用户执行--trip 31,那个么它应该和SPDEF、SPATK、SPEED、HP、ATK、DEF(任意三个)中的匹配。
"""
如果值大于28,则可以使用$project
存储在新变量中,并对每个属性执行此操作
然后用一个新的$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}}
}
]
[
{
"$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}}
}
]