title: Reduce mongodb aggregation with condition
body: <p>I have an array of objects call &quot;extra&quot; with different properties: some objects have &quot;plus&quot; and some haven't.
  I want to create inside this &quot;extra&quot; array, 2 different arrays one called &quot;cheap&quot; with all the object that don't have the &quot;plus&quot; property and one called &quot;exp&quot; with only the objects with the &quot;plus&quot; property.
  I think I can use the $reduce method in mongodb aggregate with $concatArrays and check with $cond if the property plus exists or not.
  Something like that:</p>
  <p>Data example:</p>
  <pre><code>{
      extra: [
          {
              description: &quot;laces&quot;,
              type: &quot;exterior&quot;,
              plus: '200'
          },
          {
              description: &quot;sole&quot;,
              type: &quot;interior&quot;
          },
          {
              description: &quot;logo&quot;,
              type: &quot;exterior&quot;
          },
          {
              description: &quot;stud&quot;,
              type: &quot;exterior&quot;,
              plus: '450'
          }
      ],
  }
  </code></pre>
  <pre><code> {
      $project: {
          extra: {
              $reduce: {
                  input: ['$extra'],
                  initialValue: {cheap: [], exp: []},
                  $cond: {
                      if: {$eq: ['$$this.plus', null]},
                      then: {
                          in: {
                              cheap: {
                                  $concatArrays: ['$$value.cheap', '$$this'],
                              },
                          },
                      },
                      else: {
                          in: {
                              exp: {
                                  $concatArrays: ['$$value.exp', '$$this'],
                              },
                          },
                      },
                  },
              },
          },
      },
  }    
  </code></pre>
  <p>It doesn't work...I tried many ways or writing the $cond part without luck.
  I can't figure it out.
  Thank you all.
  K.</p>