MongoDB Chart - Filter On Array Field + Unwind gets entire array

This is not exactly unexpected, but I am not sure how to deal with this problem, specifically using MongoDB Charts on Atlas.

I have some documents with an array of child objects and I set a “filter” for the Chart based on a specific field in the array. This filter DOES correctly only return records which have at least one element which contains my value, however the problem is that the array contains numerous other elements which I don’t care about. Thus when I use any field from the same array in my chart, I get a list or count of the entire array in the results instead of just the one I want.

Example below:

[
  {
    "id": 1,
    "other_thing": "whatever",
    "annoying_array": [
      {
        "sub_id": "a",
        "thing_type": "apple"
      },
      {
        "sub_id": "b",
        "thing_type": "orange"
      }
    ]
  },
  {
    "id": 2,
    "other_thing": "whatever_else",
    "annoying_array": [
      {
        "sub_id": "c",
        "thing_type": "carrot"
      },
      {
        "sub_id": "d",
        "thing_type": "pear"
      },
      {
        "sub_id": "e",
        "thing_type": "pear"
      }
    ]
  }
]

If I filter on say “pear”, I only get the second document…all good. But how do I get either the “count” of the number of times “pear” was in there or the child objects which contain pear? Again specifically in charts.

If this were a normal query I believe I would want some type of filtered projection, but is that possible with charts?

Hey @Mark_Mann, the easiest way to handle this is to put the $unwind in the query bar, rather than selecting it on the encoding card. This will ensure that the array is unwound before the filters are applied.