String_to_Date format

Hello,

I have some embedded charts and dashboards, that can be filtered based on date or date range. From the UI I’m getting date in string format. How can I change that to date type and then fetch records from my database based on the date field that I have in date format.

This is how my dashboard or chart will receive the filter values{"fromDate":"11/02/2024","toDate":"11/04/2024"} or

  1. “{"date:"11/04/2024"}”

how can do this with this option in mongodb charts:
Allowed filter fields (optional)

or is there another way to achieve this?

Thank you!

Hi @sunita_kodali -

To obtain the data for a chart, Charts runs an aggregation pipeline with multiple stages, as documented in Backing Aggregation Pipeline - Atlas Charts. Embedding filters are applied at stage 7, so if you want to convert the strings to dates you must do it in an earlier stage. This could be stage 1 (Charts View), stage 4 (query bar) or stage 6 (in-chart type conversion, which is applied alongside calculated fields).

However while all of these options should give you the behaviour you want, be warned that the performance could be very slow if your collection has a significant number of documents. This is because the database does not inherently know the date values of each string, so it needs to scan and convert every single one in order to apply the filter. You can dramatically improve filtering performance by storing the values in the collections as dates and adding an index to that field.

Tom

Thank you. I have one more question on MongoDB dashboard. We have couple charts in one dashboard, and we are trying to filter entire dashboard by date. Functionality is working great with individual chart filtering, but on a dashboard under Embed - Allow Filter Fields it is not even giving me option to select date. Where can I add the dashboard filter, just the way I do for charts.

Dashboard filtering is a different feature to embedding filters. You can access it with this button.
image

Dashboard authors can choose which fields are available for filtering, then any user can set the filter values.

Thank you, Tom.

Yes, I can do this filter from tool itself, but how can I do on authenticated embedded dashboard. We worked with authenticated embedded charts, by providing filter option, where it gave as option to select the value from pipeline. On dashboard filter, it is not giving any filed option to select.


from UI application?

When you embed charts or dashboards, filtering is only available programmatically. If you want to give your users a filtering UI, you will need to build it yourself. There is a simple example of this at https://codesandbox.io/p/sandbox/github/mongodb-js/charts-embed-sdk/tree/master/examples/dashboard/unauthenticated.

This is helpful. Thank you.