Docs Menu
Docs Home
/
MongoDB マニュアル
/

時系列コレクションへのデータの移行

項目一覧

  • 新しい時系列コレクションの作成
  • データの変換(任意)
  • 時系列コレクションへのデータの移行

既存のコレクションから時系列コレクションにデータを移行するには:

  1. 新しい時系列コレクションの作成

  2. データの変換(任意)

  3. 時系列コレクションへのデータの移行

新しい時系列コレクションを作成するには、 mongoshで次のコマンドを実行します。

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

先行コマンドの詳細については、「時系列コレクションの作成 」を参照してください。

時系列コレクションは、 metaFieldとして指定されたフィールドのセカンダリ インデックスをサポートします。 時系列データのデータモデルにメタデータ用の指定されたフィールドがない場合は、データを変換してフィールドを作成できます。 既存のコレクションのデータを変換するには、 $mergeまたは$outを使用して、時系列データを含む一時コレクションを作成します。

次の形式の気象データを含むコレクションを考えます。

{
"_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コレクションが作成されます。

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タイプではない既存のコレクションからデータを移行するには、 mongodumpmongorestoreを使用します。

警告

時系列コレクションに移行またはバックフィルする場合は、常にドキュメントを最も古いものから最も新しいものの順に挿入する必要があります。 この場合、 mongodumpはドキュメントを自然な順序でエクスポートし、 mongorestore--maintainInsertionOrderオプションは同じドキュメントの挿入順序を保証します。

たとえば、 temporarytimeseriesコレクションをエクスポートするには、次のコマンドを発行します。

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を新しいコレクション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は時系列コレクションにインデックスを作成できません。

元のコレクションにセカンダリ インデックスがある場合は、それらを手動で再作成します。 コレクションに1970-01-01T00:00:00.000Zの前、または2038-01-19T03:14:07.000Zの後にtimeField値が含まれている場合、MongoDB は警告をログに記録し、内部 クラスター化インデックスを使用する一部のクエリ最適化を無効にします。 クエリ パフォーマンスを回復し、ログ警告を解決するには、 timeFieldセカンダリ インデックスを作成します。

Tip

以下も参照してください。

時系列コレクションへのセカンダリインデックスの追加

1970-01-01T00:00:00.000Zの前または2038-01-19T03:14:07.000Zの後にtimeField値を持つドキュメントをコレクションに挿入すると、MongoDB は警告をログに記録し、一部のクエリ最適化で内部インデックスを使用できなくなります。 クエリ パフォーマンスを回復し、ログ警告を解決するには、 timeFieldセカンダリ インデックスを作成します。

戻る

時系列コレクションへのセカンダリインデックスの追加