DocumentationPluginsMongoDB data sourceMongoDB templates and variables

MongoDB templates and variables

For an introduction to templates and variables, see the following documents:

To add a new MongoDB query variable, see Add and manage variables. Use your MongoDB data source as your data source.

This is a sample query that retrieves all movie titles after 1980:

    {"$match": {year: {"$gt": 1980}}},
    {"$project": {"_id": 0, "movie_title": "$title"}} 

Compound variables

MongoDB supports compound variables, where one variable is used as multiple variables to perform complex multi-key filters.

  • Name your compound variable by starting each individual name with an underscore (_), such as _var1_var2. Do not use spaces.
  • Query for the compound variable by making the alias use the same individual names separated by a hyphen (-), such as val1-val2.
  • Call your variable by using normal variable syntax. For example, $_var1 or $_var2.

Filtering results on both movie name and year

  1. Create a variable and name it _movie_year.

  2. Select MongoDB as the data source.

  3. Get an array of items with one movie-year property by setting the query as follows:

        {"$match": {year: {"$gt": 1980}}},
        {"$project": {"_id": 0, "movie_year": {"$concat": ["$title", " - ", {"$toString":"$year"}]}}} 
    // [{"movie-year": "Ted - 2016"}, {"movie-year": "The Terminator - 1985"}]
  4. Within your query, reference $_movie and $_year as separate template variables:

    sample_mflix.movies.find({"title":"$_movie", year: $_year})
  5. Use the variable in your MongoDB queries by using Variable syntax.

Using Ad-Hoc Filters

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

Let’s look at the following query:

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

When you add a selected value, the output looks like this:


Other supported functionalities

The following functionalities with example queries are also supported:

  • ObjectId type support
sample_mflix.movies.find({"_id": ObjectId("573a1390f29313caabcd4803")})

sample_mflix.movies.find({"_id": {$in : [ObjectID("573a1391f29313caabcd6f98"), ObjectID("573a1392f29313caabcd9dee"), ObjectID("573a1392f29313caabcd9ca6")] }})
  • Regex search support

    • With double quotes:
    sample_mflix.movies.find({"title": {$regex: "ace", $options: "$i"} })
    • With single quotes:
    sample_mflix.movies.find({"title": {$regex: 'ace', $options: "$i"} })
    • With pattern
    sample_mflix.movies.find({"title": /ace/i })
  • $__timeFrom/$__timeTo macros support

sample_mflix.movies.find({"tomatoes.dvd": { $gt: $__timeFrom}}).limit(10)

sample_mflix.movies.find({ $and: [{ "tomatoes.dvd": { $gte: $__timeFrom }}, { "tomatoes.dvd": { $lt: $__timeTo }}]})
  • ISODate type support
sample_mflix.movies.find({"tomatoes.dvd": { $gte: ISODate("2013-03-01") }})

Debugging Response Size


The following feature is not currently supported in Grafana Cloud.

GF_PLUGIN_LOGGER_LEVEL can be set up as an environment variable or in the grafana.ini config file as shown in the following example:

# the log level to capture
# either 1 (traces) or 2 (debug) will work
logger_level = 2

After each query the MongoDB plugin will log information regarding the response size.