title: PyMongo: substrings match independent of words order
body: <p>I need a string search using pymongo MongoDB which check for substrings matches in a key, independently of the words order and case.</p>
  
  <p>Let's see an example.</p>
  
  <p>In my collections there are documents like the followings:</p>
  
  <pre><code>{'_id':..., 'key': 'the foo'}
  {'_id':..., 'key': 'the bar'}
  {'_id':..., 'key': 'the baz'}
  </code></pre>
  
  <p>If I searched for <code>'key'</code>, <code>'Fo tHe'</code>, <code>'foo t'</code> or <code>'foo the'</code> in the <code>key</code>, I would like to get <code>{'_id':..., 'key': 'the foo'}</code>.</p>
  
  <p>The best solution I found is using regex via pymongo in this way:</p>
  
  <pre><code>query = {'key': {'$regex' : my_string, '$options':'i'}}
  mycollection.find(query)
  </code></pre>
  
  <p>But this solution does not cover completely my requirements. E.g., it does not return the doc if <code>my_string = 'foo the'</code> (inverted word order).</p>
  
  <p>Is there an efficient way to perform that kind of text searches in pymongo (MongoDB)?</p>