title: MongoDB multiple $and operators query in PHP
body: <p>I’m having trouble with this MongoDB query using PHP array syntax. This is a direct version of the query I want to use.</p>
  
  <pre><code>db.collection.find({
          $or: [
              {$and : [{X:1}, {X: {$gt: 100}}]},
              {$and : [{X:2}, {X: {$lt: 100}}]}
          ]
  });
  </code></pre>
  
  <p><em>Note: The real query is more complicated, this is just an example.</em></p>
  
  <p>I wasn’t able to find some examples describing this kind of query in PHP.
  The best I’ve come up was this:</p>
  
  <pre><code>$query = array(
      '$or' =&gt; array(
          array(
              '$and' =&gt; array(
                  array('X' =&gt; 1),
                  array('X' =&gt; array('gt' =&gt; 100))
              )
          ),
          array(
              '$and' =&gt; array(
                  array('X' =&gt; 2),
                  array('X' =&gt; array('lt' =&gt; 100))
              )
          ),
      )
  );
  
  $this-&gt;db-&gt;collection-&gt;find($query);
  </code></pre>
  
  <p>But this query doesn't return any results.
  Obviously we can't remove <code>$and</code> from the array because we can't have duplicate keys in PHP array.</p>
  
  <p>I don't want to use JavaScript expressions because the speed is critical.</p>
  
  <p>UPDATE: As Alexander Azarov pointed out in comments, my original query can be written differently. I've updated the question with the properly used   <code>$and</code> query.</p>