MongoDb

Data SourceENTERPRISE

MongoDB Datasource
Enterprise

Enterprise plugins, along with support and assistance from the core team behind Grafana, are available with Grafana Enterprise.

Please sign up or log in to get started.

Sign Up

Grafana MongoDB Datasource Plugin

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

Usage

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
sample_mflix.movies.find()
  • Aggregate sorting - normally sorting happens with a step within the aggregate pipeline, however the MongoDB Atlas free tier doesn't allow sorting. We have expanded the syntax to allow it for those using the free tier.
    • 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.

Example:

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

Diagnostics

https://docs.mongodb.com/manual/reference/command/nav-diagnostic/

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

Examples:

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

Macros

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

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
sample_mflix.movies.aggregate([
    {"$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.

Sign up Now

Version

Dependencies:
  • Grafana 7.x.x