首页 > 解决方案 > 导出 Mongodb 图表

问题描述

我成功安装了mongodb-charts并且还能够创建仪表板。

现在我想将此仪表板保存/导出为 JSON(或任何其他格式)。是否有保存/导出和加载/导入 Mongodb 图表的功能?如果我想在其他服务器上使用相同的仪表板,这将很有用。 示例仪表板

也没有 mongodb-charts 的标签。所以任何有标签创建权限的人都可以创建标签。

标签: mongodbcharts

解决方案


MongoDB Charts 仅在 beta 版本中。

它是为 MongoDB Atlas 设计的,根据官方页面“共享仪表板和协作”,您可以通过添加新用户并在仪表板视图“访问”按钮上授予他们权限来共​​享仪表板和图表。

在访问视图中,您可以通过选择“所有人”选项并选择权限或仅与特定用户共享来使您的仪表板完全公开。

作为 hack,如果您想将仪表板转换为 JSON 格式并从一个 MongoDB Charts 实例传输到另一个实例,您可以在连接到 MongoDB Charts 的 MongoDB 实例中尝试 mongodump “元数据”数据库。

它有 4 个集合:

  • 仪表板
  • 数据源
  • 项目
  • 用户

但是所有关系都是通过 GUID id 建立的,因此如果不进行手动编辑,您很容易在 mongorestore 期间损坏数据。

更新: 以下 bash 脚本显示了如何导出仪表板和图表以将数据迁移到不同的 MongoDB Charts 实例:

# Your Dashboard and Chart names you want to export
dashboard_name="My Dashboard"
chart_name="My Chart"

# Exporting to `tmp` folder data from dashboard collection
mongodump --db metadata --collection dashboards --query "{"title": '$dashboard_name'}" --out "/tmp/"

dashboard_id=$(mongo --quiet --eval "db.getSiblingDB('metadata').dashboards.findOne({'title': '$dashboard_name'}, {'id': 1})['_id']")

# Exporting to `tmp` folder data from items collection
mongodump --db metadata --collection items --query "{\$and: [{'title': '$chart_name'}, {'dashboardId': '$dashboard_id'}]}" --out "/tmp/"

# After the following data is restored to different MongoDB Charts instance
# you need to make sure to modify the following records in imported data
# according to your datasource in new MongoDB Charts instance.

# for dashboards collection modify GUID for the following fields according to new datasource:
mongo --quiet --eval "db.getSiblingDB('metadata').dashboards.findOne({'title': '$dashboard_name'}, {'owners': 1, 'tenantId': 1, 'itemSummary.dataSourceId': 1})"

# for items collection modify GUID for the following fields according to new datasource:
mongo --quiet --eval "db.getSiblingDB('metadata').items.findOne({\$and: [{'title': '$chart_name'}, {'dashboardId': '$dashboard_id'}]}, {'dataSourceId': 1, 'tenantId': 1})"

请记住,这种方法不是官方的,可能会损坏您的数据。


推荐阅读