Query for documents in the last 24h in charts

Hi there,

I’d like to create a chart in my dashboard where I show the number of documents created per hour in the last 24 hours (and also last week). Each document has a timestamp field with the date created. I don’t want to update the query every time.

So I would imagine a query like

{ timestamp: {$gte: {ISODate() - 7*24*60*60*1000}}}

but this is not supported.

I know things like that are possible in code. Also I found $dateSubtract, but that’s aggregation only.

I don’t seem to be able to edit the raw aggregation of the chart either, although I can view it.

Could somebody point me in the right direction? I feel like this could be a common use case.


You can use simple JavaScript expressions in Charts filters. You just need to wrap everything in new Date(...) to ensure the calculated value is of the expected type.

Alternatively, rather than write an MQL query, you can add a date filter and use the Relative or Period options.

1 Like

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