如果您的数据集存储着需要跨时间间隔进行比较的数据,请使用时间序列数据集来提升性能并节省存储空间。有关时间序列数据集优势的更多信息,请参阅“时间序列数据集”部分。
关于此任务
本页面介绍如何迁移已存在于 MongoDB 集合中的数据。如果您的数据位于关系数据库中,请使用关系迁移器将数据迁移到时间序列集合中。
You can use the $out aggregation stage to migrate data from an existing collection into a time series collection. MongoDB does not guarantee output order when you migrate data with $out. To maintain order, sort your data before you migrate it with an aggregation pipeline.
开始之前
考虑一个包含时间和元数据信息的 weather_data 集合:
步骤
有关迁移数据的其他注意事项的更多信息,请参阅时间序列数据集的最佳实践。
注意
To migrate your time series data with mongodump and mongorestore instead, run mongorestore with the --noIndexRestore option, because mongorestore can't create indexes on time series collections. To preserve the time ordering of your measurements, also run the command with the --maintainInsertionOrder option so that documents are inserted in order, from oldest to newest.
后续步骤
如果原始集合中有二级索引,请现在手动重新创建。
如果时间序列集合在 1970-01-01T00:00:00.000Z 之前或 2038-01-19T03:14:07.000Z 之后包含 timeField 个值,MongoDB 会记录警告并禁用某些使用内部聚集索引的查询优化。要恢复查询性能并解决日志警告问题,请在 timeField 上创建二级索引。