将数据迁移到时间序列集合
要将现有集合中的数据迁移到时间序列集合,请在聚合管道中使用 $out
阶段。
将数据迁移到时间序列集合
1
(可选)转换数据以创建元数据字段(如果不存在)。此字段不是必填字段。
如果原始数据集没有元数据字段,可使用 $addFields
聚合阶段添加元数据字段。
考虑使用以下格式的天气数据集合:
{ "_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 } }
以下管道阶段添加了 metaData
字段,并使用 $project
包含或排除文档中的其余字段:
{ $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 } }
2
将时间序列选项与 $out 聚合阶段结合使用
下面的示例使用了 db.collection.aggregate()
助手方法。有关聚合阶段语法,请参阅 $out
。有关时间序列选项的完整说明,请参阅时间序列字段参考。
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: { db: "mydatabase", coll: "weathernew", timeseries: { timeField: "ts", metaField: "metaData" } } } ])
运行该命令后,就会出现以下 weathernew
集合:
db.weathernew.findOne() { "_id" : ObjectId("5553a998e4b02cf7151190b8"), "ts" : ISODate("1984-03-05T13:00:00Z"), "metaData" : { "st" : "x+47600-047900", "position" : { "type" : "Point", "coordinates" : [ -47.9, 47.6 ] }, "elevation" : 9999, "callLetters" : "VCSZ", "qualityControlProcess" : "V020", "type" : "FM-13" }, "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 } }
如果原始集合中有二级索引,请现在手动重新创建。如果集合在 1970-01-01T00:00:00.000Z
之前或 2038-01-19T03:14:07.000Z
之后包含 timeField
个值,MongoDB 会记录警告并禁用某些使用内部聚集索引的查询优化。在 timeField
上创建二级索引,以恢复查询性能并解决日志警告问题。