title: combining two $elemMatch queries with AND boolean operator in MongoDB
body: <p>I have two distinct <em>mongoDB</em> queries that represent two different conditions, for example:</p>
  
  <pre><code>{ stuff: { $elemMatch: { foo: 1, bar: "a" } } }
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>{ stuff: { $elemMatch: { foo: 2, bar: "b" } } }
  </code></pre>
  
  <p>where <code>stuff</code> is an array of elements that have both <code>foo</code> and <code>bar</code> fields set.</p>
  
  <p>Now, I am not sure how to match elements in the collection that meet at the same time the two aforementioned conditions.</p>
  
  <p>Just to be clear: in this case I need to get all elements that have both one element of <code>stuff</code> that has  <code>foo</code> set as <code>1</code> with <code>bar</code> set as <code>"a"</code> and also one element of <code>stuff</code> that has <code>foo</code> set as <code>2</code> with <code>bar</code> set as <code>"b"</code>.</p>
  
  <p>Doing  <code>{ stuff: { $elemMatch: { foo: { $in: [1, 2] }, bar: { $in: ["a", "b"] } } }</code> is wrong, since it will behave like an <code>OR</code> on the two expression (and including two new expressions).</p>
  
  <p>Any idea on how to <code>AND</code> them?</p>