Reputation: 137
I have a web page where users can see data from MongoDB on a map. I want to have several check boxes, radio buttons, etc to filter what is seen on the map. If I was using MySQL I would do
query = "SELECT * FROM table WHERE x = 1"
if checkbox == "checked":
query += "AND WHERE y = 2"
How can I replicate that with pymongo?
Upvotes: 0
Views: 4652
Reputation: 9578
You simply build up the query dict instead:
query = {'x': 1}
if checkbox == 'checked':
query['y'] = 2
results = db.collection.find(query)
Doing an OR
query would look like this:
query = [{'x': 1}]
if checkbox == 'checked':
query.append({'y': 2})
results = db.collection.find({'$or': query})
Upvotes: 6