Plugins 〉MongoDB

Data Source


  • Overview
  • Installation
  • Change log
  • Related content

Grafana MongoDB Datasource Plugin

The Grafana MongoDB Datasource allows you to visualize data from MongoDB in Grafana.


Query Editor

The query editor supports the same syntax as the MongoDB Shell, with some limitations:

  • You can only run one command/query.
  • Only read commands are supported: find and aggregate
  • Most Object constructors are not supported (with the exception of ISODate, which is supported)

The editor expands upon the MongoDB Shell syntax in the following ways.

  • Database selection - you can supply the name of the database in place of the normal "db":
    • NOTE: you can still use "db". It will refer to the default database in your connection string
  • Aggregate sorting - normally sorting happens with a step within the aggregate pipeline. We have expanded the syntax to allow it on aggregate in a similar fashion to find
    • NOTE: MongoDB doesn't perform the sort with this syntax, it happens after the results are queried from the collection.
sample_mflix.movies.aggregate({}).sort({"time": 1})

Intellisense/Code completion

  • With a blank editor, Ctrl + Space will show a selection of all the available databases.
  • Entering a dot after the database will show a selection of all the available collections for that database.
  • Entering a dot after the collection will show the available query methods.
  • Entering a dot after the query method will show additional functions: sort/limit

Running the query

Cmd + S will run the query

Time Series

When visualizing time series data, the plugin needs to know which field to use as the time. Simply project the field with a name alias of "time". The field data type must be a date.

You can coerce non-date data types to date. Doing so will allow using non-date fields as the time series time. The example below show how to convert the int field "year" to a date that is projected as "time" using the MongoDB $dateFromParts pipeline operator.


{"$match": { "year": {"$gt" : 2000} }},
{"$group": { "_id": "$year", "count": { "$sum": 1 }}},
{"$project": { "_id": 0, "count": 1, "time": { "$dateFromParts": {"year": "$_id", "month": 2}}}}
).sort({"time": 1})

If you want to group your time series by Metric, project a field called "__metric".

Example: Show count of movies over time by movie rating

{"$match": { "year": {"$gt" : 2000}}},
{"$group": { "_id": {"year":"$year", "rated":"$rated"}, "count": { "$sum": 1 } }},
{"$project": { "_id": 0, "time": { "$dateFromParts": {"year": "$_id.year", "month": 2}}, "__metric": "$_id.rated", "count": 1}}
).sort({"time": 1})


The following diagnostic commands are currently supported: "stats", "serverStatus", "replSetGetStatus", "getLog", "connPoolStats", "connectionStatus", "buildInfo", "dbStats", "hostInfo", "lockInfo"


admin.connectionStatus()  // run the connectionStatus command
admin.connectionStatus({"authInfo.authenticatedUserRoles": 1})  // run and only return the "authInfo.authenticatedUserRoles" field
admin.connPoolStats({arg: "pool"})  // run the connPoolStats command and pass 1 argument
admin.serverStatus({args: {repl: 0, metrics:0}})  // run the serverStatus command and pass multiple args


You can reference the dashboard time range in your queries.

  • $__timeFrom - a macro that references the dashboard start time
  • $__timeTo - a macro that references the dashboard end time
sample_mflix.movies.find({released: {$gt: "$__timeFrom"}}).sort({year: 1})

Template Variables

Interval Variables

Interval variables can be converted to milliseconds by applying the suffix "_ms" to the variable name.

Example: "1m" will be converted to "60000"

Compound Variables

MongoDB supports the idea of "Compound Variables", which allows using one variable as multiple variables to perform complex multi-key filters.

To create a Compound Variable, use the naming convention of breaking the variables up using underscores (must start with underscore): _var1_var2 When querying, the response must be in the format: val1-val2

Example: I want to filter results on both movie name and year.

  1. Create variable of type Query: _movie_year
  2. Set the variable query to a query that will return an array of items with one movie-year property:
// Example
    {"$match": {year: {"$gt": 2011}}},
    {"$project": {"_id": 0, "movie_year": {"$concat": ["$title", " - ", {"$toString":"$year"}]}}} 

// [{"movie-year": "Ted - 2016"}, {"movie-year": "The Terminator - 1985"}]

  1. Now in your query, you can reference "Movie" and "Year" as separate template variables using the syntax "$_variable":
// Example
sample_mflix.movies.find({"title":"$_movie", year: $_year})

Using Ad-Hoc Filters

In addition to the standard "ad-hoc filter" type variable of any name, a second helper variable must be created. It should be a "constant" type with the name mongodb_adhoc_query and a value compatible with the query editor. The query result will be used to populate the UI's selectable filters. You may choose to hide this variable from view as it serves no further purpose.

A query of:

sample_mflix.movies.aggregate([ {"$group": { "_id": "$year"}},  {"$project": { "year": "$_id", "_id": 0 }} ] )

…and a value selected, would look like: ad_hoc_filter

Furthermore, import this Ad-Hoc Dashboard to see an example.

For more information, visit the docs on plugin installation.

Change Log


[1.1.6] 2021-4-28

  • Fix - Handle nullable Boolean values

[1.1.5] 2021-4-9

  • Chore - update enterprise and plugin sdk

[1.1.4] 2021-2-6

  • Feature - Time Series grouping by __metric
  • Feature - Convert Interval variables to ms with _ms suffix

[1.1.1] 2021-1-25

  • Workaround - Build with Go 1.14.14 to workaround old common name certs

[1.1.0] 2021-1-19

  • Feature - TLS/SSL support

[1.0.3] 2021-1-8

  • Bug Fix - Time Macros in Aggregation #46

[1.0.2] 2020-10-28

  • Signing issue

[1.0.1] 2020-10-23

  • Fix Readme badge/links

[1.0.0] 2020-10-23

  • Initial release