Docs 菜单
Docs 主页
/
MongoDB Manual
/

将数据迁移到时间序列集合

在此页面上

  • 创建新的时间序列集合
  • 转换数据(可选)
  • 将数据迁移到时间序列集合

要将数据从现有集合迁移到 时间序列集合:

  1. 创建新的时间序列集合

  2. 转换数据(可选)

  3. 将数据迁移到时间序列集合

要创建新的时间序列集合,请在 mongosh中发出以下命令:

db.createCollection(
"weathernew", {
timeseries: {
timeField: "ts",
metaField: "metaData",
granularity: "hours"
}
}
)

有关上述命令的更多信息,请参阅创建time-series collection。

时间序列集合支持在指定为metaField的字段上建立二级索引。 如果您的 time-series 数据的 Realm 数据模型没有为元数据指定字段,您可以转换数据以创建一个字段。要转换现有collection中的数据,请使用$merge$out创建包含time-series数据的临时collection。

考虑具有以下格式的天气数据的collection:

{
"_id" : ObjectId("5553a998e4b02cf7151190b8"),
"st" : "x+47600-047900",
"ts" : ISODate("1984-03-05T13:00:00Z"),
"position" : {
"type" : "Point",
"coordinates" : [ -47.9, 47.6 ]
},
"elevation" : 9999,
"callLetters" : "VCSZ",
"qualityControlProcess" : "V020",
"dataSource" : "4",
"type" : "FM-13",
"airTemperature" : { "value" : -3.1, "quality" : "1" },
"dewPoint" : { "value" : 999.9, "quality" : "9" },
"pressure" : { "value" : 1015.3, "quality" : "1" },
"wind" : {
"direction" : { "angle" : 999, "quality" : "9" },
"type" : "9",
"speed" : { "rate" : 999.9, "quality" : "9" }
},
"visibility" : {
"distance" : { "value" : 999999, "quality" : "9" },
"variability" : { "value" : "N", "quality" : "9" }
},
"skyCondition" : {
"ceilingHeight" : { "value" : 99999, "quality" : "9", "determination" : "9" },
"cavok" : "N"
},
"sections" : [ "AG1" ],
"precipitationEstimatedObservation" : { "discrepancy" : "2", "estimatedWaterDepth" : 999 }
}

要转换此数据,我们发出以下命令:

db.weather_data.aggregate([
{
$addFields: {
metaData: {
"st": "$st",
"position": "$position",
"elevation": "$elevation",
"callLetters": "$callLetters",
"qualityControlProcess": "$qualityControlProcess",
"type": "$type"
}
},
}, {
$project: {
_id: 1,
ts: 1,
metaData: 1,
dataSource: 1,
airTemperature: 1,
dewPoint: 1,
pressure: 1,
wind: 1,
visibility: 1,
skyCondition: 1,
sections: 1,
precipitationEstimatedObservation: 1
}
}, {
$out: "temporarytimeseries"
}
])

运行此命令后,您将拥有一个中间temporarytimeseries集合:

db.temporarytimeseries.findOne()
{
"_id" : ObjectId("5553a998e4b02cf7151190b8"),
"ts" : ISODate("1984-03-05T13:00:00Z"),
"dataSource" : "4",
"airTemperature" : { "value" : -3.1, "quality" : "1" },
"dewPoint" : { "value" : 999.9, "quality" : "9" },
"pressure" : { "value" : 1015.3, "quality" : "1" },
"wind" : {
"direction" : { "angle" : 999, "quality" : "9" },
"type" : "9",
"speed" : { "rate" : 999.9, "quality" : "9" }
},
"visibility" : {
"distance" : { "value" : 999999, "quality" : "9" },
"variability" : { "value" : "N", "quality" : "9" }
},
"skyCondition" : {
"ceilingHeight" : { "value" : 99999, "quality" : "9", "determination" : "9" },
"cavok" : "N"
},
"sections" : [ "AG1" ],
"precipitationEstimatedObservation" : { "discrepancy" : "2", "estimatedWaterDepth" : 999 },
"metaData" : {
"st" : "x+47600-047900",
"position" : {
"type" : "Point",
"coordinates" : [ -47.9, 47.6 ]
},
"elevation" : 9999,
"callLetters" : "VCSZ",
"qualityControlProcess" : "V020",
"type" : "FM-13"
}
}

要将数据从非timeseries类型的现有集合迁移到时间序列集合,请使用mongodumpmongorestore

警告

迁移或回填到时间序列集合时,应始终按从旧到新的顺序插入文档。 在这种情况下, mongodump按自然顺序导出文档, mongorestore--maintainInsertionOrder选项保证文档的插入顺序相同。

例如,要导出temporarytimeseries collection,请发出以下命令:

mongodump
--uri="mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/weather" \
--collection=temporarytimeseries --out=timeseries

该命令返回以下输出:

2021-06-01T16:48:39.980+0200 writing weather.temporarytimeseries to timeseries/weather/temporarytimeseries.bson
2021-06-01T16:48:40.056+0200 done dumping weather.temporarytimeseries (10000 documents)

要将timeseries/weather/temporarytimeseries.bson导入新集合weathernew ,请发出以下命令:

mongorestore
--uri="mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/weather" \
--collection=weathernew --noIndexRestore \
--maintainInsertionOrder \
timeseries/weather/temporarytimeseries.bson

该命令返回以下输出:

2021-06-01T16:50:56.639+0200 checking for collection data in timeseries/weather/temporarytimeseries.bson
2021-06-01T16:50:56.640+0200 restoring to existing collection weather.weathernew without dropping
2021-06-01T16:50:56.640+0200 reading metadata for weather.weathernew from timeseries/weather/temporarytimeseries.metadata.json
2021-06-01T16:50:56.640+0200 restoring weather.weathernew from timeseries/weather/temporarytimeseries.bson
2021-06-01T16:51:01.229+0200 no indexes to restore
2021-06-01T16:51:01.229+0200 finished restoring weather.weathernew (10000 documents, 0 failures)
2021-06-01T16:51:01.229+0200 10000 document(s) restored successfully. 0 document(s) failed to restore.

注意

确保使用--noIndexRestore选项运行上述命令。 mongorestore无法在时间序列集合上创建索引。

如果原始集合中有二级索引,请现在手动重新创建。如果集合在 1970-01-01T00:00:00.000Z 之前或 2038-01-19T03:14:07.000Z 之后包含 timeField 个值,MongoDB 会记录警告并禁用某些使用内部聚集索引的查询优化。在 timeField创建二级索引,以恢复查询性能并解决日志警告问题。

提示

另请参阅:

向时间序列集合添加从节点索引

如果在1970-01-01T00:00:00.000Z之前或2038-01-19T03:14:07.000Z之后将文档插入到具有timeField值的集合中,MongoDB 会记录警告并阻止某些查询优化使用内部索引。 在timeField创建二级索引,以恢复查询性能并解决日志警告问题。

后退

向时间序列集合添加从节点索引