Array is being converted into an indexed object, which breaks my MongoDB aggregation

  • Goal:

Pass an array to a MongoDB aggregation so I can use the array as the value of an $and operator

  • Details:

In my JS script I create a variable:

  const datesArray = [
    { $gte: { $date: `${selectedYear}-${datesObj.start}` } },
    { $lte: { $date: `${selectedYear}-${datesObj.end}` } },
  ];

And pass it into my query via

    aggregateBrokerCommissionReports.trigger({
      additionalScope: {
        datesArray
      },
      onSuccess: (data) => {
        resolve(data);
      },
    });

Then my aggregation attempts to use it as the value of the $and operator

  {
    $match: {
      $and: {{ datesArray }}
    }
  },

I receive an error because the array is converted into an object with index keys so mongoDB doesn't know what to do with it

image

How do I force retool to maintain the Array type and not convert my array to an object?

Your aggregation is using datesArray and not data?
You can also try {{formatDataAsArray(datesArray)}} if datesArray is in fact what is being used in the aggregation