Docs Menu

Time Series 컬렉션으로 데이터 마이그레이션

기존 컬렉션의 데이터를 Time Series 컬렉션으로 마이그레이션하려면:

  1. 새 time series 컬렉션 만들기

  2. 데이터 변환(선택 사항)

  3. Time Series 컬렉션으로 데이터 마이그레이션

time series 컬렉션 을 만들려면 mongosh에서 다음 명령을 실행합니다.

db.createCollection(
"weathernew", {
timeseries: {
timeField: "ts",
metaField: "metaData",
granularity: "hours"
}
}
)

앞의 명령에 대한 자세한 내용 은 Time Series 컬렉션 만들기를 참조하세요.

time series 컬렉션은 metaField 로 지정된 필드의 보조 인덱스 를 지원합니다. time-series 데이터의 Realm 데이터 모델에 메타데이터용으로 지정된 필드가 없는 경우 데이터를 변환하여 생성할 수 있습니다. 기존 collection의 데이터를 변환하려면 $merge 또는 $out 을(를) 사용하여 time-series 데이터로 임시 collection을 만듭니다.

다음 형식의 날씨 데이터가 포함된 collection을 가정해 보겠습니다.

{
"_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 }
}

이 데이터를 변환하기 위해 다음 명령을 실행합니다.

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 collection이 생성됩니다.

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 유형이 아닌 기존 컬렉션의 데이터를 time series 컬렉션 으로 마이그레이션하려면 mongodumpmongorestore 를 사용합니다.

경고

time series 컬렉션으로 마이그레이션하거나 백필할 때는 항상 가장 오래된 문서부터 최신 문서까지 순서대로 삽입해야 합니다. 이 경우 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)

timeseries/weather/temporarytimeseries.bson 을(를) 새 collection 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.

참고

앞의 명령은 --noIndexRestore 옵션과 함께 실행해야 합니다. mongorestore 은 time series 컬렉션에 인덱스를 만들 수 없습니다.

원래 컬렉션에 세컨더리 인덱스가 있는 경우 지금 수동으로 다시 생성합니다.