Hi, I am having trouble in MongoDB Charts via Atlas. It seems like the chart fields is unable to determine the field type array.

Aggregation:

[
  {
    $match: {
      events: { $ne: [] }
    }
  },
  {
    $project: {
      createdAt: 1,
      updatedAt: 1,
      phoneNumber: 1,
      events: 1, // field is an array of objects
      sjEvents: {
        $ifNull: [
          {
            $filter: {
              input: "$events",                 
              as: "event",                       
              cond: { $eq: ["$$event.service", "Testing"] } // Filter condition
            }
          },
          []  // if there are no matching events, return an empty array
        ]
      }
    }
  }
]

Results in Data Services:
image

Results in Charts:
image

Problem:
In Data Service, sjEvents fields are properly returned as array in which I can access/view the objects. While in the Charts, it seems like it is conflicted between string datatype and array datatype. This makes me unable to access the object’s data to use for the charts.

I hope there is a solution to this issue, thank you in advance.

Hi @Nurain_Nabilah_Salehuddin -

This is most likely happening because when Charts is sampling the collection to determine the fields and types, the majority of occurrences of sjEvents were empty arrays ([]).

You should be able to force Charts to discover the underlying fields by choosing Add Field > Missing Field and then entering a path like sjEvents.event.

Tom