Integrate MongoDB to Grafana
This plugin enables you to query and visualize data from your MongoDB databases directly within Grafana. Leverage the flexibility of MongoDB's aggregation pipeline to create insightful dashboards and panels.
- Flexible Querying: Query data using MongoDB's aggregation pipeline syntax in JSON or JavaScript. Support query variables to create dynamic dashboards.
- Time Series & Table Data: Visualize time-based data or display results in tabular format for various Grafana panels.
- MongoDB Atlas Support Connect to MongoDB Atlas Services.
- Legacy Plugin Compatibility: Easy migrate from the legacy plugin with support for its query syntax.
- No authentication
- Username/Password authentication
Run the script quick_start.py in the root directory to start MongoDB and Grafana containers with the plugin
python3 scripts/quick_start.py
-
Download: Obtain the latest plugin build from the Release page or workflow artifacts.
-
Install:
- Extract the downloaded archive (
haohanyang-mongodb-datasource-<version>.zip
) into your Grafana plugins directory (/var/lib/grafana/plugins
or similar). - Ensure the plugin binaries (
mongodb-datasource/gpx_mongodb_datasource_*
) have execute permissions (chmod +x
). - Configure the plugin as a data source within Grafana, providing your MongoDB connection details.
- Extract the downloaded archive (
Refer to the example docker-compose.prod.yaml file for a production-ready setup.
- Start Querying:
- Select your MongoDB data source in a Grafana panel.
- Use the query editor to write your aggregation pipeline queries (see Query Language below).
Provide the collection name and your MongoDB aggregation pipeline in standard JSON format.
Example: Retrieve 10 AirBnB listings scraped within the selected time range:
[
{
"$match": {
"last_scraped": {
"$gt": {
"$date": {
"$numberLong": "$__from"
}
},
"$lt": {
"$date": {
"$numberLong": "$__to"
}
}
}
}
},
{
"$limit": 10
}
]
- Legacy: Maintain compatibility with the older plugin's syntax:
This gives the same result as the previous JSON query.
db.listingsAndReviews.aggregate([ /* Your aggregation pipeline (JSON) */ ]);
db.listingsAndReviews.aggregate([ { "$match": { "last_scraped": { "$gt": { "$date": { "$numberLong": "$__from" } }, "$lt": { "$date": { "$numberLong": "$__to" } } } } }, { "$limit": 10 } ])
- ShadowRealm (Secure): Define an
aggregate()
function that returns your pipeline. The function executes within a ShadowRealm sandboxed environment.In this example, only the admin user to can view the query result.function aggregate() { // ... your logic based on template variables ... return [ /* Your aggregation pipeline */ ]; }
function aggregate() { const user = "${__user.login}" let filter = {} if (user !== "admin") { filter = { noop: true } } return [ { $match: filter }, { $limit: 10 } ] }
-
Time series: For time-based visualizations. Your query must return documents with
ts
(timestamp) andvalue
fields. An optionalname
field enables grouping by category.The following query of Sample AirBnB Listings Dataset shows the number of AirBnB listings in each month that have the first review in the selected time range.
[ { "$match": { "first_review": { "$gt": { "$date": { "$numberLong": "$__from" } }, "$lt": { "$date": { "$numberLong": "$__to" } } } } }, { "$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: For more flexible data display in tables, pie charts, etc. No specific output schema is required.
BSON Type | Support | Go Type | Notes |
---|---|---|---|
Double | ✅ | float64 | |
String | ✅ | string | |
Object | ✅ | json.RawMessage | May be converted to string if necessary |
Array | ✅ | json.RawMessage | May be converted to string if necessary |
ObjectId | ✅ | string | |
Boolean | ✅ | bool | |
Date | ✅ | time.Time | |
Null | ✅ | nil | |
32-bit integer | ✅ | int32 | May be converted to int64/float64 |
64-bit integer | ✅ | int64 | May be converted to float64 |
Note: Unsupported BSON types are not included in the table and will display as "[Unsupported type]"
.
Apache-2.0