2 回答
TA贡献1777条经验 获得超10个赞
编辑:
对于多个过滤器:
filter = {}
if price is not None: filter['price'] = {'$lt' : price }
if quantity is not None: filter['quantity'] = {'$lt' : quantity }
if volume is not None: filter['volume'] = {'$lt' : volume }
posts = db.testcollection.find(filter)
原件:
试试这个,以免自己在布尔逻辑世界中有点发疯:
if price is None:
filter = {}
else:
filter = {'price' : {'$lt' : price }}
posts = db.collection.find(filter)
TA贡献1883条经验 获得超3个赞
如果你想从前端传递一个数组(使用查询参数),你可以使用 mongodb 的 $in 参数: URL 参数在数组中作为 JSON 对象发送:
/read?job_type%5B%5D=Full%20Time&job_type%5B%5D=Part%20Time&country%5B%5D=Germany&country%5B%5D=China
@app.route("/read", methods=['GET', 'POST'])
def read():
job_type = request.args.getlist('job_type[]')
country = request.args.getlist('country[]')
filter = {}
if len(job_type) > 0: filter['job_type'] = {'$in': job_type}
if len(country) > 0: filter['country'] = {'$in': country}
jobs = db_operations.find(filter)
添加回答
举报