AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

Database Tools を使用してデータを時系列コレクションに移行する

mongodump と mongorestore を使用して次の手順に従って既存コレクションのデータを時系列コレクションに移行します。

1

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

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

この例では、timeField、metaField、およびgranularity のサンプル データを使用します。前のコマンドの詳細については、「時系列コレクションの作成」をご覧ください。

2

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

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

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 } })

注意

時系列 metaField および grandularity として適切なフィールドを選択することで、ストレージとクエリのパフォーマンスの両方を最適化できます。フィールド選択とベストプラクティスの詳細については、「metaFieldと粒度のベストプラクティス」を参照してください。

以下のパイプラインは、次の操作を実行します。

  • $addFields を使用して、metaData フィールドを weather_data コレクションに追加します。

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

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" }}
3

timeseries タイプではない既存のコレクションからデータをエクスポートするには、mongodump を使用します。

警告

時系列コレクションに移行またはバックフィルする場合は、常に古いものから新しいものの順にドキュメントを挿入してください。この場合、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.bson2021-06-01T16:48:40.056+0200 done dumping weather.temporarytimeseries(10000 documents)
4

データを時系列コレクションにインポートするには、mongorestore を使用します。

重要

mongorestore コマンドを --noIndexRestore オプション付きで実行することを確認してください。mongorestore は時系列コレクションにインデックスを作成することができません。

次の操作は、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.bson2021-06-01T16:50:56.640+0200 restoring to existing collection weather.weathernew without dropping2021-06-01T16:50:56.640+0200 reading metadata for weather.weathernew from timeseries/weather/temporarytimeseries.metadata.json2021-06-01T16:50:56.640+0200 restoring weather.weathernew from timeseries/weather/temporarytimeseries.bson2021-06-01T16:51:01.229+0200 no indexes to restore2021-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にセカンダリ インデックスを作成します。

Tip

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

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