How do you query MongoDB by pairs of keys using pymongo
<p>I have a list of pairs of things, of the form e.g. <code>[['A', 'B'], ['C', 'D']]</code>. I want to query <code>MongoDB</code> for records in a specific collection which have properties matching both of these things.</p>

<p>For example, here is what I would want to get back:</p>

<pre><code>[{'_id': ObjectId('...'),
  'first_property': 'A',
  'second_property': 'B'
 },
 {'_id': ObjectId('...'),
  'first_property': 'C',
  'second_property': 'D'
 }]
</code></pre>

<p>How do I query <em>simultaneous</em> properties using <code>pymongo</code>?</p>

<p>I got pretty good performance out of the following pattern:</p>

<pre><code>client.find({'$or': [{'property_a': value_a, 'property_b': value_b} for value_a, value_b in some_list_of_two_element_tuples]}
</code></pre>

<p>This creates a very long <a href="https://docs.mongodb.com/manual/reference/operator/query/or/" rel="nofollow"><code>$or</code></a> statement out of our requested tuples.</p>

python mongodb pymongo