【问题标题】:Using pymongo/mongodb to filter based on multiple conditions including and/or?使用 pymongo/mongodb 根据多个条件进行过滤,包括和/或?
【发布时间】:2021-07-13 23:31:16
【问题描述】:

我正在使用 pymongo 教程和 Zeppelin 笔记本中的餐厅数据库。

  1. 哪些餐厅是比萨店,并且在 成绩低于 C 或分数高于 19,不分年级?

我已尝试同时使用 $or/$and 语句,但我不断收到错误

%mongodb

db.resturants.find({$and :[{"cuisine": {$regex: "Pizza"}},
                           {"grades.grade" : {$ne: "A"}},
                           {"grades.grade" : {$ne: "B"}},
                           {"grades.grade" : {$ne: "C"}}]},
                   {$or : [{"cuisine": {$regex: "Pizza"}},
                           {"grades.score" : {$gt: 19}}]}).table()

【问题讨论】:

    标签: python mongodb pymongo


    【解决方案1】:

    你需要为你的操作符使用字符串:

    db.resturants.find({"$and" :[{"cuisine": {$regex: "Pizza"}},
                               {"grades.grade" : {$ne: "A"}},
                               {"grades.grade" : {$ne: "B"}},
                               {"grades.grade" : {$ne: "C"}}]},
                       {"$or" : [{"cuisine": {$regex: "Pizza"}},
                               {"grades.score" : {$gt: 19}}]}).table()
    

    您的语法在 MongoDB 中有效,但 pymongo 必须使用字符串。

    【讨论】:

    • 这不提供任何输出。如果只包含不带引号的 $and 语句,它可以工作,但在 $and 中添加引号会导致它不再输出任何内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多