wwli
wwli

Reputation: 2681

python/mongodb pymongo : nested find()/filter

I would like to do something like this

 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})

contents would contain the final filtered result. Is it doable in python with mongodb?

Upvotes: 1

Views: 885

Answers (1)

alecxe
alecxe

Reputation: 474281

How about doing it this way:

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)

Hope that helps.

Upvotes: 1

Related Questions