title: How to create proper filters with Flask and PyMongo?
body: <p>My first post here :)</p>
  
  <p>I'm using Flask and PyMongo for my site and I want the user to have an ability to filter database results. To start simple:</p>
  
  <p>Basic filter would return results based on the price argument. If the price is None then I want to return all the results.</p>
  
  <p>From MongoDB docs:</p>
  
  <blockquote>
    <p>The $or operator performs a logical OR operation on an array of two or more expressions and selects the documents that satisfy at least one of the expressions</p>
  </blockquote>
  
  <pre class="lang-py prettyprint-override"><code>   price = request.args.get('price')
     if price:
         price = int(price)
  
     posts = db.collection.find({ '$or' : [
                                  {'price' : {'$lt' : price }},
                                  {'price' : {'$ne' : None }}
                                  ]
                                })
  </code></pre>
  
  <p>My idea was that if the price is not an integer (None) that the $or operator wouldn't satisfy first expression and move to the second one returning all that are not equal to None. However this doesn't really work and I can't figure any other way to display 'all' results when the argument is not passed or when it is wrong.</p>
  
  <p>Also a bit borader questions - what if I want to have multiple filters? Should I create one big query for the database using $and operator and manipulate variable values in order to get the desired result? Or is there a better way? The only thing I thought about was a route for each filter but this doesn't sound like something that would work. Any suggestions are welcome! Thank you!</p>