title: in a mongo query, how to display in an array only the elements that match a filter?
body: <p>here is a table of objects:</p>
  <pre><code>[
      {
          field0: value0,
          field1: value1,
          field2: {
              field3: value3,
              field4 : [
                  { sfield1: svalue110, sfield2: svalue210 },
                  { sfield1: svalue111, sfield2: svalue211 },
              ]
          }
      },
      {
          field0: value0,
          field1: value1,
          field2: {
              field3: value3,
              field4 : [
                  { sfield1: svalue120, sfield2: svalue220 },
                  { sfield1: svalue111, sfield2: svalue211 },
                  { sfield1: svalue122, sfield2: svalue222 },
              ]
          }
      },
      {
          field0: value0,
          field1: value1,
          field2: {
              field3: value3,
              field4 : [
                  { sfield1: svalue130, sfield2: svalue230 },
                  { sfield1: svalue131, sfield2: svalue231 },
                  { sfield1: svalue132, sfield2: svalue232 },
                  { sfield1: svalue133, sfield2: svalue233 },
              ]
          }
      }
  ]
  </code></pre>
  <p>I want to apply a filter on sfield1 and sfield2 and display only the objects for which field4 has a size &gt; 0. And inside field4, display only the objects that match the filter.</p>
  <p>for example, I want to display only the objects for which sfield1 = svalue111 and sfield2 = svalue211</p>
  <p>I must have this result:</p>
  <pre><code>[
      {
          field0: value0,
          field1: value1,
          field2: {
              field3: value3,
              field4 : [
                  { sfield1: svalue111, sfield2: svalue211 },
              ]
          }
      },
      {
          field0: value0,
          field1: value1,
          field2: {
              field3: value3,
              field4 : [
                  { sfield1: svalue111, sfield2: svalue211 },
              ]
          }
      }
  ]
  </code></pre>
  <p>I tried addFields and reduce this way:</p>
  <pre><code>{
      $addFields: {
          &quot;field2.field4&quot;: {
              $reduce: {
                  input: '$field2.field4',
                  initialValue: [],
                  in: {
                      $let: {
                          vars: {
                              ee: {
                                  $filter: {
                                      input: '$$this.field4',
                                      as: 'z',
                                      cond: {
                                          $and: [
                                              {
                                                  $eq: [
                                                      '$$z.sfield1',
                                                      &quot;svalue111&quot;
                                                  ]
                                              },
                                              { $eq: ['$$z.sfield2', &quot;svalue211&quot;] }
                                          ]
                                      }
                                  }
                              }
                          },
                          in: {
                              $cond: [
                                  { $ne: [0, { $size: '$$ee' }] },
                                  {
                                      $concatArrays: [
                                          '$$value',
                                          [
                                              {
                                                  $mergeObjects: [
                                                      '$$this',
                                                      { &quot;field2.field4&quot;: '$$ee' }
                                                  ]
                                              }
                                          ]
                                      ]
                                  },
                                  '$$value'
                              ]
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  <p>but i have this error :</p>
  <pre><code>Error: command failed: {
      &quot;ok&quot; : 0,
      &quot;errmsg&quot; : &quot;Invalid $addFields :: caused by :: FieldPath field names may not contain '.'.&quot;,
      &quot;code&quot; : 16412,
      &quot;codeName&quot; : &quot;Location16412&quot;
  } : aggregate failed :....
  </code></pre>
  <p>thank you for your help</p>