python / mongodb pymongo:嵌套的find()/ filter

时间:2013-06-10 22:11:15

标签: python mongodb pymongo

我想做这样的事情

 contents = contents.find() # get all from collection
if user filled search box 1:
     contents = contents.find({'field1':seached_var})
if user filled search box 2:
     contents = contents.find({'field2':seached_var2})

内容将包含最终过滤结果。 用mongodb在python中可行吗?

1 个答案:

答案 0 :(得分:1)

这样做怎么样:

conditions = {}
if user filled search box 1:
     conditions['field1'] = seached_var
if user filled search box 2:
     conditions['field2'] = seached_var2

contents = contents.find(conditions)

希望有所帮助。