How to pass aggregate query while embedding MongoDB chart to pre-process the data?

Hi, I am using ChartsEmbedSDK to embed a chart I have created using authenticated JWT token. In MongoDB atlas, I have entered an aggregate query in the query bar to pre-process data.

Now, while embedding the chart, the aggregate query has some dynamic values which I want to pass from my app. I could not get the reference to this use-case anywhere in documentation so I tried passing aggregate query in the filter key.
This is my code:

this.sdk = new ChartsEmbedSDK({
      baseUrl: 'BASE_URL',
      height: 500,
      filter: [{
		$match: matchQuery }
	}, {
		$unwind: unwindQuery
	}, {
		$project: projectQuery
	}, {
		$group: groupQuery
      getUserToken: () => {
        return token

    this.chart = this.sdk.createChart({
      chartId: 'CHART_ID'

    this.chart.render(document.getElementById("element_id")).catch(() => window.alert('Chart failed to initialise')); 

I have added the field inside $match query in the User Specified filters in Authenticated Embed chart.

But, I am still getting this error: Error loading data for this chart (error code: 7). User filter is not allowed.

Hi @Rahul_Chouhan1,

Welcome to MongoDB community.

I don’t think you can pass an aggregation pipeline as a filter.

The way to try and do that is to use a pipeline in your data source and build the data in a way that you pass field filtering as a plain simple query.


This is how I am processing my data in charts.

Now, if I write this aggregate query in the pipeline in the data source as you suggested, I need to pass an array of ObjectIds as you can see in the image, like this

_user: { $in: [ObjectID1, ObjectID2, ...] }

So, the problem is

  1. How do I write query in pipeline so that I can take the array of ObjectIDs as an input?
  2. How do I pass the same input from the SDK?
  3. Can I perform all the operations shown in image ($match, $unwind, $project, $group, $in, $cond, $gte, $sum, $last) because none of them is mentioned in this documentation Data Source Pipelines?

It will be really helpful if you explain with an example.

Hi @Rahul_Chouhan1 -

Right now there are a few limitations in what you can do here:

  1. With the embedding SDK, you can only pass in a filter (which is wrapped in a $match stage), no other stages can be injected.
  2. The full aggregation pipeline is generated in a specific order as documented at Backing Aggregation Pipeline — MongoDB Charts

This means that while it is possible for you to pass in your ObjectID query through the SDK, it will be inserted after the query bar query, the $project and $group stages will execute before the filter (which probably isn’t what you want).

Grouping and projections are automatically generated when you encode fields, so perhaps you could find a way to make this work. We do realise the current behaviour can be limiting for advanced scenarios, and we hope to find ways to make it more flexible in the future.