Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个网页,用户可以在其中在地图上查看来自 MongoDB 的数据。我想要几个复选框、单选按钮等来过滤地图上看到的内容。如果我使用 MySQL,我会这样做
query = "SELECT * FROM table WHERE x = 1" if checkbox == "checked": query += "AND WHERE y = 2"
如何用 pymongo 复制它?
您只需构建查询字典:
query = {'x': 1} if checkbox == 'checked': query['y'] = 2 results = db.collection.find(query)
进行OR查询将如下所示:
OR
query = [{'x': 1}] if checkbox == 'checked': query.append({'y': 2}) results = db.collection.find({'$or': query})