Docs Menu
Docs Home
/ / /
Go Driver
/

時系列コレクション

項目一覧

  • Overview
  • 時系列コレクションの作成
  • 時系列コレクションをクエリする
  • 詳細情報

このガイドでは、MongoDB の時系列コレクションと、MongoDB Go ドライバーでの時系列コレクションの操作方法を学習できます。

時系列コレクションは一定期間にわたる測定値のシーケンスを効率的に保存します。 コレクションは、次の情報を含む時系列データで構成されています。

  • 一定時間にわたって収集されたデータ

  • 測定を説明するメタデータ

  • 測定日

測定値
Metadata

販売データ

収益

会社

影響率

影響を受けた人口の量

ロケーション

重要

時系列コレクションには MongoDB 5.0以降が必要です。

時系列コレクションを作成するには、次のパラメータを CreateCollection()メソッドに渡します。

  • 作成する新しいコレクションの名前

  • 少なくとも時間フィールドを指定するTimeSeriesOptionsオブジェクト

次の例では、 dbデータベースにtemperatureを時間フィールドとして持つmarch2022時系列コレクションを作成します。

db := client.Database("db")
// Creates a time series collection that stores "temperature" values over time
tso := options.TimeSeries().SetTimeField("temperature")
opts := options.CreateCollection().SetTimeSeriesOptions(tso)
db.CreateCollection(context.TODO(), "march2022", opts)

コレクションを作成したかどうかを確認するには、 "listCollections"コマンドをRunCommand()メソッドに送信します。

package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
func main() {
var uri string
if uri = os.Getenv("MONGODB_URI"); uri == "" {
log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/ja-jp/docs/drivers/go/current/usage-examples/")
}
client, err := mongo.Connect(options.Client().ApplyURI(uri))
if err != nil {
panic(err)
}
defer client.Disconnect(context.TODO())
db := client.Database("myDB")
// Creates a command to list collections
command := bson.D{{"listCollections", 1}}
var result bson.M
// Runs the command on the database
commandErr := db.RunCommand(context.TODO(), command).Decode(&result)
if commandErr != nil {
panic(commandErr)
}
// Prints the command results
output, outputErr := json.MarshalIndent(result, "", " ")
if outputErr != nil {
panic(outputErr)
}
fmt.Printf("%s\n", output)
}
{
...
"cursor": {
"firstBatch": [
{
"info": {
"readOnly": false
},
"name": "march2022",
"options": {
"timeseries": {
...
}
},
"type": "timeseries"
},
...
}

時系列コレクションをクエリするには、 がデータを取得して集計するのと同じ規則を使用します。

上記で説明されている操作の詳細については、次のガイドを参照してください。

  • 時系列コレクション

  • 時系列コレクションの制限

  • コマンドの実行

  • Retrieve Data

このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。

戻る

GridFS