title: Mongodb match empty object in nested document
body: <p>I'm just wondering if this is possible to do in a single request?</p>
  
  <p>Given</p>
  
  <pre><code>{
     _id: 1,
     foo: {
       fred: {},          // &lt;- I want to remove empty keys like this
       barney: { bar: 1 } // &lt;- But keep these keys
     }
  }
  </code></pre>
  
  <p>Expected</p>
  
  <pre><code>{
     _id: 1,
     foo: {
       barney: { bar: 1 }
     }
  }
  </code></pre>
  
  <p>I know how to do it in several requests, but I'm trying to understand MongoDB better.</p>
  
  <hr>
  
  <p><strong>Note.</strong> <code>fred</code> becomes empty in update command like <code>{ $unset: { "fred.baz": 1 } }</code> when <code>baz</code> is the last key in <code>fred</code>.</p>
  
  <p>Maybe it is possible to remove it with its contents? But the command sender does not know, is there any other keys, except <code>baz</code> at the moment.</p>