title: Ignore null values when sorting by array in MongoDB
body: <p>I have MongoDB collection <code>items</code> with following documents:</p>
  <pre><code>{ &quot;values&quot; : [1, 2] }
  { &quot;values&quot; : [5, null] }
  { &quot;values&quot; : [-5, null] }
  </code></pre>
  <p>Is there any way to sort this collection by <code>values</code> property without <code>null</code> values? My current query is:</p>
  <pre><code>db.items.aggregate([{&quot;$sort&quot;: {&quot;values&quot;: 1}}])
  </code></pre>
  <p>with this result:</p>
  <pre><code>{ &quot;values&quot; : [5, null] }
  { &quot;values&quot; : [-5, null] }
  { &quot;values&quot; : [1, 2] }
  </code></pre>
  <p>First values are <code>[5, null]</code> and <code>[-5, null]</code> because <code>null</code> is smallest value in collection. However, I would like to ignore null values and sort by numbers only, so:</p>
  <pre><code>{ &quot;values&quot; : [-5, null] }
  { &quot;values&quot; : [1, 2] }
  { &quot;values&quot; : [5, null] }
  </code></pre>
  <p>When there is no number (both values are <code>null</code>), document should be last in result.</p>