meln5674 / grafana-mongodb-community-plugin

Open Source Grafana Plugin for querying MongoDB
GNU Affero General Public License v3.0
130 stars 17 forks source link

Access the date picker of Grafana #19

Closed Kromtar closed 1 year ago

Kromtar commented 1 year ago

Is there any way to incorporate the selected date range from Grafana's date picker into the aggregation query?

meln5674 commented 1 year ago

Yes, the start and end dates are provided as the $__from and $__to variables which can be included in your query, as described here . As well, if you use the timeseries query type, there is an option to automatically inject a $match stage before your pipeline which uses these variables, assuming your timestamp is a standard ISODate() object.

Kromtar commented 1 year ago

Thank very much.

Here is an example of how to retrieve documents within a time range using Grafana's Picker. Assume that the 'timestamp' field is where we store the time record in Date format:


[{
  "$match":{
    "$expr":{
      "$and":[
        {"$gte":["$timestamp",{"$toDate":"${__from:date}"}]},
        {"$lte":["$timestamp",{"$toDate":"${__to:date}"}]}
      ]
    }
  }
}]