Py学习  »  Tunmee  »  全部回复
回复总数  1
6 年前
回复了 Tunmee 创建的主题 » 我能按字典的键查询MongoDB吗?

您可以编写一个简单的函数,将键列表转换为键到通配符值的映射。这是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"])
})