Mongodb chart filter not working via the SDK

Hi there,

I’m new to MongoDB charts and have done a search for answers via the community but cannot find the one I am looking for.

I have set up my chart as unauthenticated and using 2 fields to display data:

  • rating
  • date

I also want to filter via the field ‘user’ which contains the user id for the logged-in user.

If I filter this via the MongoDB chart dashboard it works fine. But if I set this via the SDK, it doesn’t show the chart at all on my web application.

I’ve tried to test this using:

*filter: { “user”: { “$match”: ‘63c18143c9379120a15b935e’ } }
*chart.setFilter({“user”: { “$match”: ‘63c18143c9379120a15b935e’ }}.

I’ve tried without the $match for both also {“user”: “63c18143c9379120a15b935e”} and nothing is displayed.

It works fine without setting the filters.

Can someone help and explain what I’m doing wrong, do I need a token?

Many thanks

Kirstie

Here’s the code:

  const sdk = new ChartsEmbedSDK({
    baseUrl: 'This is where I've placed my Chart url'
  });

  const chart = sdk.createChart({ chartId: 'This is where I've placed my chart id',
  height: "400px",
  width:"100%",
  showAttribution:false,
  background:"transparent",
  labels:false,
  autoRefresh:true,
  filter: { "user": { "$match": '63c18143c9379120a15b935e' } }

}); 

async function refresh(){
  await chart.render(document.getElementById('chart'));
  await chart.setAutoRefresh(true);
  await chart.setMaxDataAge(30);
}

refresh()

Hi @Kirstie_Hayes -

You don’t want to include the $match in the query.
Assuming the value you are searing on is an ObjectID, you’ll either need to decorate it like this:

 {“user”: { $oid: “63c18143c9379120a15b935e”} }

Or I think you can use a BSON library to instantiate an ObjectID instance directly.

Tom

Hi @tomhollander

It is an ObjectID and that worked, thanks so much for your help.

Kirstie

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.