Database Tools를 사용하여 Time Series 컬렉션으로 데이터 마이그레이션
다음 단계에 따라 mongodump
와 mongorestore
를 사용하여 기존 컬렉션에서 Time Series 컬렉션으로 데이터를 마이그레이션합니다.
단계
새로운 Time Series 컬렉션을 생성합니다.
새로운 Time Series 컬렉션을 생성하려면 mongosh
:에서 다음 명령을 실행합니다.
db.createCollection( "weathernew", { timeseries: { timeField: "ts", metaField: "metaData", granularity: "hours" } } )
이 예시에서는 timeField
, metaField
및 granularity
에 대한 샘플 데이터를 사용합니다. 이전 명령에 대한 자세한 내용은 Time Series 컬렉션 생성을 참조하세요.
(선택 사항) 데이터를 변환합니다.
Time Series 컬렉션은 metaField
로 지정된 필드에 보조 인덱스를 지원합니다. Time Series 데이터 모델에 메타데이터를 위한 지정된 필드가 없는 경우 데이터를 변환하여 해당 필드를 생성할 수 있습니다. 기존 컬렉션의 데이터를 변환하려면 $out
을 사용하여 Time Series 데이터가 포함된 임시 컬렉션을 생성합니다.
다음 형식의 날씨 데이터가 포함된 collection을 가정해 보겠습니다.
db.weatherdata.insertOne( { _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 } } )
참고
Time Series metaField
및 grandularity
로 적절한 필드를 선택하면 저장과 쿼리 성능을 모두 최적화할 수 있습니다. 필드 선택과 모범 사례에 대한 자세한 정보는 metaField 및 세분성 모범 사례를 참조하세요.
아래 파이프라인은 다음 작업을 수행합니다.
$addFields
를 사용하여weather_data
컬렉션에metaData
필드를 추가합니다.$project
를 사용하여 문서의 나머지 필드를 포함하거나 제외합니다.$out
을 사용하여temporarytimeseries
라는 임시 컬렉션을 생성합니다.
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
유형이 아닌 기존 컬렉션에서 데이터를 내보내려면 mongodump
를 사용하세요.
경고
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)
컬렉션을 가져옵니다.
Time Series 컬렉션에 데이터를 가져오려면 mongorestore
를 사용하세요.
중요
mongorestore
명령을 --noIndexRestore
옵션과 함께 실행해야 합니다. mongorestore
는 Time Series 컬렉션에 인덱스를 생성할 수 없습니다.
다음 작업은 timeseries/weather/temporarytimeseries.bson
을 새 컬렉션 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.
원래 컬렉션에 세컨더리 인덱스가 있는 경우 지금 수동으로 다시 생성합니다. 컬렉션에 1970-01-01T00:00:00.000Z
이전 또는 2038-01-19T03:14:07.000Z
이후의 timeField
값이 포함되어 있는 경우, MongoDB는 경고를 기록하고 내부 클러스터 인덱스를 사용하는 일부 쿼리 최적화를 비활성화합니다. timeField
에 세컨더리 인덱스를 만들어 쿼리 성능을 회복하고 로그 경고를 해결합니다.
팁
1970-01-01T00:00:00.000Z
이전 또는 2038-01-19T03:14:07.000Z
이후에 timeField
값이 있는 문서를 컬렉션에 삽입하면 MongoDB는 경고를 기록하고 일부 쿼리 최적화에서 내부 인덱스를 사용하지 못하도록 합니다. timeField
에 세컨더리 인덱스를 생성하여 쿼리 성능을 회복하고 로그 경고를 해결합니다.