Inject Filter Per User

Are there any examples online on how to use the ‘Inject Filter Per User’ functionality? I set up authentication using the example in GitHub - mongodb-js/charts-embed-sdk: The easiest way to embed MongoDB Charts visualisations into your web app, and it all worked fine. When I tried to configure the setting, using return { ownerId: context.token.sub };, the chart didn’t include any data.

Hey @Brian_Hnat2 -

There are some docs on this feature at Filter Embedded Charts — MongoDB Charts. The key thing to understand is that the return value of the function needs to be a MQL query document which makes sense for your particular collection. In the example you sent, the function will take the sub value from the JWT token (let’s say it’s “Charty” for the current user) and uses it to construct a filter on the ownerId field. So the result will be that you embedded chart’s pipeline will include this near the beginning:

{ $match: { ownerId: "Charty"} }

If your collection does not have an ownerId field, or if it exists but no documents match the value from the token’s sub field, you’ll get an empty chart. I suspect you may need to change the function to match the way your collection is set up.

HTH
Tom

1 Like

That helps, thanks. I added an ownerId field to my datasource, and the filter worked as expected. That’s the part that I wasn’t clear on - between the datasource, and additional query/aggregation used, and any calculated fields, where/how the injected MQL document was used.

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