使用以下步骤将数据从现有集合迁移到带有 mongodump
和 mongorestore
的时间序列集合。
步骤
创建一个新的时间序列集合。
要创建新的 时间序列集合,请在 mongosh
中发出以下命令:
db.createCollection( "weathernew", { timeseries: { timeField: "ts", metaField: "metaData", granularity: "hours" } } )
此示例使用 timeField
、metaField
和 granularity
的示例数据。有关前述命令的更多信息,请参阅创建时间序列集合。
(可选)转换您的数据。
时间序列集合支持在指定为 metaField
的字段上创建二级索引。如果时间序列数据的数据模型没有为元数据指定字段,您可以转换数据来创建一个指定字段。要转换现有集合中的数据,请使用 $out
创建一个包含时间序列数据的临时集合。
考虑具有以下格式的天气数据的collection:
db.weatherdata.insertOne( { _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 } } )
注意
选择正确的字段作为时间序列 metaField
和 grandularity
可优化存储和查询性能。有关字段选择和最佳实践的更多信息,请参阅 metaField 和粒度最佳实践。
以下管道执行以下操作:
使用
$addFields
将metaData
字段添加到weather_data
集合。使用
$project
包含或排除文档中的其余字段。使用
$out
来创建一个名为temporarytimeseries
的临时集合。
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
类型的现有集合中导出数据,请使用 mongodump
。
警告
在迁移或追溯填充时间序列集合时,您应始终按从最旧到最新的顺序插入文档。在这种情况下,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)
导入您的集合。
要将数据导入时间序列集合,请使用 mongorestore
。
重要
确保运行带有 --noIndexRestore
选项的 mongorestore
命令。mongorestore
无法在时间序列集合上创建索引。
以下操作将 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.
如果原始集合中有二级索引,请现在手动重新创建。如果集合在 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
上创建二级索引,以恢复查询性能并解决日志警告问题。