/mongodb-datasource

Simple MongoDB datasource for Grafana

Primary LanguageGoApache License 2.0Apache-2.0

Grafana MongoDB data source

example branch parameter

This plugin provides a Grafana datasource for querying and visualizing data from MongoDB.

screenshot

Download

Download the latest build

Use

Query language

The query text should be a valid MongoDB Aggregate pipeline - an array consisting of MongoDB Aggregate operations. Your may use the Grafana's built-in variables "$__from" and "$__to" to query data based on the current panel's time range. The plugin supports JSON and JavaScript query languages. In JSON query, you need to enter the database in the UI. Here is an example of JSON query.

[
    {
        "$match": {
            "createdTime": {
                "$gt": {
                    "$numberLong": "$__from"
                },
                "$lt": {
                    "$numberLong": "$__from"
                }
            }
        }
    }
]

In JavaScript query, you need to follow the format db.<collection-name>.aggregate([<json-query>]). As shows in the following example.

db.transactions.aggregate({
    "$match": {
        "createdTime": {
            "$gt": {
                "$numberLong": "$__from"
            },
            "$lt": {
                "$numberLong": "$__from"
            }
        }
    }
});

You can also use the "$from" and "$to" conventions originated from a legacy plugin

[
    {
        "$match": {
            "createdTime": {
                "$gt": "$from",
                "$lt": "$to"
            }
        }
    }
]

Query type

Time series

Time series query type is suitable for time series panels. The query results should consist the following fields:

  • ts The timestamp
  • value Either integer or double. Currently all values should be of the same type.
  • name (Optional) Useful if you want to show time series data of different categories.

Here is an example of JSON query from Sample AirBnB Listings Dataset. The query shows the number of created AirBnB reviews of apartment and house property type in each month during the selected time range.

[
    {
        "$match": {
            "first_review": {
                "$gt": {
                    "$date": {
                        "$numberLong": "$__from"
                    }
                },
                "$lt": {
                    "$date": {
                        "$numberLong": "$__to"
                    }
                }
            },
            "property_type": {
                "$in": [
                    "Apartment",
                    "House"
                ]
            }
        }
    },
    {
        "$group": {
            "_id": {
                "month": {
                    "$dateToString": {
                        "format": "%Y-%m",
                        "date": "$first_review"
                    }
                },
                "property_type": "$property_type"
            },
            "value": {
                "$count": {}
            }
        }
    },
    {
        "$project": {
            "ts": {
                "$toDate": "$_id.month"
            },
            "name": "$_id.property_type",
            "value": 1
        }
    }
]

table

Table type is more flexible and doesn't require the output schema. This usually fits panels that don't require timestamp.

Install

  • Download the packaged plugin haohanyang-mongodb-datasource-<version>.zip from workflow artifacts to the root directory (where docker-compose.yaml exists) and extract files to folder mongodb-datasource
unzip haohanyang-mongodb-datasource-<version>.zip -d mongodb-datasource
  • Grant 0755(-rwxr-xr-x) permission to binaries in mongodb-datasource directory.
chmod 0755 mongodb-datasource/gpx_mongodb_datasource_*