具有多个条件的Python MongoDB动态查询构建

具有多个条件的Python MongoDB动态查询构建,python,mongodb,flask,flask-pymongo,Python,Mongodb,Flask,Flask Pymongo,我正在使用Python MongoDB ORM(特别是Flask)构建一个开源项目,我在构建动态条件方面有点困难 下面的代码是我在相应文件中编写的 Model.py from app.database import Database class Model: conditions = {"and":[], "or":[], "in":[]} operators = { "!=": "$ne", "<": "$lt", "&

我正在使用Python MongoDB ORM(特别是Flask)构建一个开源项目,我在构建动态条件方面有点困难

下面的代码是我在相应文件中编写的

Model.py

from app.database import Database

class Model:

    conditions = {"and":[], "or":[], "in":[]}
    operators = {
        "!=": "$ne",
        "<": "$lt",
        ">": "$gt",
        "<=": "$lte",
        ">=": "$gte",
        "in": "$in",
        "not in":"$nin",
        "and": "$and",
        "or": "$or"
    }

    def __init__(self):
        # collection property from User class
        # Database class takes collection to fire MongoDB queries 
        self.db = Database(self.collection)

    def where(self, field, operator, value=None):
        if value is None:
            # to enable Model.where("first_name", "John")
            value = operator
            operator = "="

        self._handle_condition("and", field, operator, value)
        # to enable Model.where().where_or() and etc
        return self

    def where_or(self, field, operator, value=None):
        if value is None:
            # to enable Model.where("first_name", "John")
            value = operator
            operator = "="

        self._handle_condition("or", field, operator, value)
        # to enable Model.where().where_or() and etc
        return self

   def _handle_condition(self, type, field, operator, value):
        self.conditions[type].append({"field":field, "operator":operator, value:value})

   def get(self):
       filetrs = {}
       for type in self.conditions:
           filetrs[self.operators[type]] = []
           for condition in self.conditions[type]:
               if condition["operator"] == "=":
                   filter = {condition["field"]:condition["value"]}
               else:
                   filter = {condition["field"]:{self.operators[condition["operator"]]:condition["value"]}}
               filetrs[self.operators[type]].append(filter)
       return self.db.find(filters)
from app.Model import Model

class UserModel(Model):
    # MongoDB collection name
    collection = "users"

    def __init__(self):
        Model.__init__(self)

User = UserModel()
from app.User import User

class UserController:
    def index(self):
        # Should return all the users where _id is not blank or their first_name is equal to John
        return User.where("_id", "!=", "").where_or("first_name", "John").get()
我想要实现的是,从
UserController.py
导入
User.py
并像前面提到的代码一样使用

当使用
Where
Where\u或
Model方法添加多个条件时,
get
方法解析所有条件,并作为过滤器传递给
find
方法

UserController.py

from app.database import Database

class Model:

    conditions = {"and":[], "or":[], "in":[]}
    operators = {
        "!=": "$ne",
        "<": "$lt",
        ">": "$gt",
        "<=": "$lte",
        ">=": "$gte",
        "in": "$in",
        "not in":"$nin",
        "and": "$and",
        "or": "$or"
    }

    def __init__(self):
        # collection property from User class
        # Database class takes collection to fire MongoDB queries 
        self.db = Database(self.collection)

    def where(self, field, operator, value=None):
        if value is None:
            # to enable Model.where("first_name", "John")
            value = operator
            operator = "="

        self._handle_condition("and", field, operator, value)
        # to enable Model.where().where_or() and etc
        return self

    def where_or(self, field, operator, value=None):
        if value is None:
            # to enable Model.where("first_name", "John")
            value = operator
            operator = "="

        self._handle_condition("or", field, operator, value)
        # to enable Model.where().where_or() and etc
        return self

   def _handle_condition(self, type, field, operator, value):
        self.conditions[type].append({"field":field, "operator":operator, value:value})

   def get(self):
       filetrs = {}
       for type in self.conditions:
           filetrs[self.operators[type]] = []
           for condition in self.conditions[type]:
               if condition["operator"] == "=":
                   filter = {condition["field"]:condition["value"]}
               else:
                   filter = {condition["field"]:{self.operators[condition["operator"]]:condition["value"]}}
               filetrs[self.operators[type]].append(filter)
       return self.db.find(filters)
from app.Model import Model

class UserModel(Model):
    # MongoDB collection name
    collection = "users"

    def __init__(self):
        Model.__init__(self)

User = UserModel()
from app.User import User

class UserController:
    def index(self):
        # Should return all the users where _id is not blank or their first_name is equal to John
        return User.where("_id", "!=", "").where_or("first_name", "John").get()
问题是,它在任何一种情况下都不起作用,
where
where\u或
但当我尝试添加多个
where
where\u或
条件时,它似乎不起作用

非常感谢你的帮助

PS:这个问题似乎有很多代码,但为了让你理解我必须要做的全部场景,如果你还需要任何澄清,请随时发表评论

热切地期待着