您可以编写一个简单的函数,将键列表转换为键到通配符值的映射。这是Javascript中的一个示例(抱歉,我不是python用户):
function mapKeys (keys) {
const mappedKeys = keys.map(function(key) {
return {
// Note that I'm using the $exists here instead of $gt as
// $exists actually checks if the field exists
[`mydict.${key}`]: { "$exists": true }
}
})
return mappedKeys;
}
c.find(query={
"$or": mapKeys(["foo","bar","xxx","yyy","zzz"])
})