title: MongoDB Differentiates Between undefined vs. null
body: <p>I was checking logic for querying on non-values and noticed when using the <code>mongo</code> shell, it differentiates between <code>undefined</code> and <code>null</code> values.</p>
  <pre><code>&gt; use test
  &gt; db.test.insert({ a : 1,          b : null,       c : undefined })
  &gt; db.test.insert({ a : null,       b : undefined,  c : 1 })
  &gt; db.test.insert({ a : undefined,  b : 1,          c : null })
  </code></pre>
  <p>When you query on the collection, you get this:</p>
  <pre><code>&gt; db.test.find()
  { &quot;_id&quot; : ObjectId(&quot;52d95575c9333565e80ccb22&quot;), &quot;a&quot; : 1, &quot;b&quot; : null, &quot;c&quot; : null }
  { &quot;_id&quot; : ObjectId(&quot;52d9557fc9333565e80ccb23&quot;), &quot;a&quot; : null, &quot;b&quot; : null, &quot;c&quot; : 1 }
  { &quot;_id&quot; : ObjectId(&quot;52d95586c9333565e80ccb24&quot;), &quot;a&quot; : null, &quot;b&quot; : 1, &quot;c&quot; : null }
  </code></pre>
  <p>When you query on <code>null</code> however, it only retrieves records who was explicitly set to <code>null</code>.</p>
  <pre><code>&gt; db.test.find({ a : null })
  { &quot;_id&quot; : ObjectId(&quot;52d9557fc9333565e80ccb23&quot;), &quot;a&quot; : null, &quot;b&quot; : null, &quot;c&quot; : 1 }
  </code></pre>
  <p>Is this a bug in MongoDB?  <strike>How can I properly query for null/ undefined/ non-set fields?</strike></p>
  <h1>EDIT</h1>
  <p>So I can query for not-set values with this:</p>
  <pre><code>db.test.find({ $or : [ { a : null }, { a : { $exists : false } } ] })
  </code></pre>
  <p>But in this example though, it still only returns the single record:</p>
  <pre><code>{ &quot;_id&quot; : ObjectId(&quot;52d9557fc9333565e80ccb23&quot;), &quot;a&quot; : null, &quot;b&quot; : null, &quot;c&quot; : 1 }
  </code></pre>
  <p>Anyone know why/ how MongoDB differentiates between <code>undefined</code> and <code>null</code>?  Is this an issue with how the data was entered in MongoDB?</p>