title: Is it possible to default a value in Mongodb like $ifNull but with empty string?
body: <p>A mongo operator I love to use is <code>$ifNull</code>, but sometimes this doesn't cover all the cases I need to cover, like for example an empty string. Is there an operator that works like the <code>$ifNull</code> operator (but with other conditions getting checked, like empty string), all in a single step? I just need to default the value if it's null or empty string. Below is a snippet of a query I'm doing currently:</p>
  <pre class="lang-json prettyprint-override"><code>//ex docs:
  {
    &quot;_id&quot;: &quot;1&quot;,
    &quot;field&quot;: {
      &quot;value&quot;: &quot;test&quot;
    }
  },
  {
    &quot;_id&quot;: &quot;2&quot;,
    &quot;field&quot;: {
      &quot;value&quot;: &quot;&quot;
    }
  },
  {
    &quot;_id&quot;: &quot;3&quot;
    &quot;field&quot;: {
      &quot;value&quot;: null
    }
  }
  </code></pre>
  <pre class="lang-js prettyprint-override"><code>//step in query where I'm checking ifNull:
  ...
  {
      $project: {
          resValue: {
              $ifNull: [&quot;$field.value&quot;, &quot;No Data&quot;]
          },
      }
  }
  ...
  </code></pre>
  <p>I'm trying to google around but see a lot of solutions involving $function, or just matching out the results with empty strings. This doesn't work in my case because I need all the documents included, but just need to default the resValue to &quot;No Data&quot; if it's empty or null. I'm also trying to limit the complexity and keep the query as efficient as possible, so I figure it's best not to include a $function step before the project.</p>
  <p>Currently running version 4.4.13 on my db.</p>
  <p>I need to return the value of the field itself if it exists and isn't an empty string, but if it's empty string or null, return &quot;No Data&quot; or some other default string as resValue on the resultant doc.</p>