- Introduction to MongoDB >
- Databases and Collections >
- Time Series Collections >
- Migrate Data into a Time Series Collection
Migrate Data into a Time Series Collection¶
On this page
To migrate data from an existing collection into a time series collection:
- Create a New Time Series Collection
- Transform Data (Optional)
- Migrate Data into a Time Series Collection
Create a New Time Series Collection¶
To create a new time series collection, issue the following command in the
mongosh
:
For more information on the preceeding command, see Create a Time Series Collection.
Transform Data (Optional)¶
Time series collections support secondary indexes on the field specified as the
metaField
. If the data model of your time series data does not have
a designated field for your metadata, you can transform your data to
create one. To transform the data in your existing collection, use
$merge
or $out
to create a temporary collection
with your time series data.
Consider a collection with weather data of the following format:
To transform this data, we issue the following command:
After you run this command, you have an intermediary
temporarytimeseries
collection:
Migrate Data into a Time Series Collection¶
To migrate your data from an existing collection that is not of type
timeseries
into a time series collection, use mongodump
and
mongorestore
.
Warning
When migrating or backfilling into a time series collection you
should always insert the documents in order, from oldest to newest.
In this case mongodump
exports documents in natural
order and the --maintainInsertionOrder
option for
mongorestore
guarantees the same insertion order for
documents.
For example, to export the temporarytimeseries
collection, issue the
following command:
The command returns the following output:
To import timeseries/weather/temporarytimeseries.bson
into the new
collection weathernew
, issue the following command:
The command returns the following output:
Note
Ensure that you run the preceeding command with the
--noIndexRestore
option.
mongorestore
cannot create indexes on time series
collections.
If your original collection had secondary indexes, manually recreate them now.