Migrate Data into a Time Series Collection将数据迁移到时间序列集合
On this page本页内容
To migrate data from an existing collection into a time series collection, use an 要将数据从现有集合迁移到时间序列集合,请在聚合管道中使用$out
stage in your aggregation pipeline.$out
阶段。
In MongoDB versions prior to 7.0.3, an aggregation pipeline cannot use 在7.0.3之前的MongoDB版本中,聚合管道不能使用$out
to output to a time series collection. $out
输出到时间序列集合。To migrate data into a time series collection with MongoDB versions prior to 7.0.3, use 要使用7.0.3之前的MongoDB版本将数据迁移到时间序列集合中,请使用mongodump
and mongorestore
.mongodump
和mongorestore
。
Migrate Data to a Time Series Collection将数据迁移到时间序列集合
(Optional) Transform your data to create a metadata field if one doesn't exist. This field is not required.(可选)如果不存在元数据字段,则转换数据以创建元数据字段。此字段不是必需字段。
If the original collection doesn't have a metadata field, use the 如果原始集合没有元数据字段,请使用$addFields
aggregation stage to add it.$addFields
聚合阶段进行添加。
Consider a collection with weather data that uses the format:考虑使用以下格式集合天气数据:
{
"_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 }
}
The following pipeline stages add a 以下管道阶段添加metaData
field and use $project
to include or exclude the remaining fields in the document: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
}
}
Use the timeseries option with the $out aggregation stage将时间序列选项与$out
聚合阶段一起使用
The example below uses the 下面的示例使用db.collection.aggregate()
helper method. db.collection.aggregate()
辅助方法。For the aggregation stage syntax, see 有关聚合阶段语法,请参阅$out
. For a full explanation of the time series options, see the Time Series Field Reference.$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"
}
}
}
])
After you run this command, you have the 运行此命令后,您将获得下面的weathernew
collection below: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 }
}
If your original collection had secondary indexes, manually recreate them now. If your collection includes 如果您的原始集合具有辅助索引,请立即手动重新创建它们。如果您的集合包含timeField
values before 1970-01-01T00:00:00.000Z
or after 2038-01-19T03:14:07.000Z
, MongoDB logs a warning and disables some query optimizations that make use of the internal clustered index. 1970-01-01T00:00:00.000Z
之前或2038-01T03:14:07.000Z
之后的timeField
值,MongoDB会记录一条警告并禁用一些使用内部聚集索引的查询优化。Create a secondary index on the 在timeField
to regain query performance and resolve the log warning.timeField
上创建辅助索引以恢复查询性能并解决日志警告。
See also: 另请参阅:
Add Secondary Indexes to Time Series Collections向时间序列集合添加辅助索引