Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

Charts with a dynamic filter in aggregate queries

I have an aggregate query in Charts that uses a $facet to group values. The $match in the facet stage uses dates as it’s filter. Is there any way to insert these dates from a embedded chart implemention?

I know I can set the &filter variable in the payload request, however I believe this only filters at the top level?

My issue is that records contain two dates (one started and one finished). In my match statement (could be replaced by the filter value) I look at the startdate being between two dates or the finishdate between two dates. In the facet I do a match on the startdate being between the two dates (so count as started) and the finishdate being between two dates (so count as finished). How would I solve that in a single query in Charts (for use with embedding)?

Hi @Arnold_Ligtvoet -

Unfortunately I can’t think of a way of doing this. The filter applied via the query string parameter is always used in a dedicated $match stage, after the query bar filter. There’s no way to inject this inside your $facet stage. It is possible to do basic date arithmetic in the query bar using Javascript, so if you wanted your embedded charts to use a dynamic but predictable date range like “within the last 30 days” you could do that without needing to use embedding filters.

Tom

Hi Tom, thanks for confirming the issue. Having a basic predictable date range won’t work in my use case, as we have integrated the charts in a portal where the user can select a date range. I think my main issue it trying to do counts based on two different variables with dates. In the initial $match fase provided by the charts filter option I have an or statement, so that will work. I need to split these later on in two different counts (hence the $facet). Perhaps using the inserted filters later on (like $$filter1 and $$filter2) would be a nice feature addition?

Added a feature request: https://feedback.mongodb.com/forums/923524-charts/suggestions/40541110-possibility-to-use-filter-parameters-in-later-stag