Docs Menu
Docs Home
/ / /
Go 드라이버
/

Time Series 컬렉션

이 페이지의 내용

  • 개요
  • Time Series 컬렉션 만들기
  • 시계열 컬렉션 쿼리
  • 추가 정보

이 가이드에서는 MongoDB의 time series 컬렉션에 대해 알아보고, MongoDB Go 드라이버에서 time series 컬렉션과 상호 작용하는 방법을 알아봅니다.

time series 컬렉션은 일정 기간 동안의 측정 시퀀스를 효율적으로 저장합니다. 이 collection은 다음 정보가 포함된 time-series 데이터로 구성됩니다.

  • 시간 경과에 따라 수집된 데이터

  • 측정값을 설명하는 메타데이터

  • 측정 날짜

예시
측정
Metadata

판매 데이터

수익

회사

감염률

감염자 수

위치

중요

Time series 컬렉션에는 MongoDB 5.0 이상이 필요합니다.

time series 컬렉션을 만들려면 CreateCollection() 메서드에 다음 매개변수를 전달합니다.

  • 생성할 새 컬렉션의 이름

  • 최소한 시간 필드를 지정하는 TimeSeriesOptions 객체

다음 예에서는 temperature 를 시간 필드로 사용하여 db 데이터베이스에 march2022 time series 컬렉션을 만듭니다.

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)

컬렉션을 생성했는지 확인하려면 RunCommand() 메서드에 "listCollections" 명령을 보냅니다.

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/ko-kr/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"
},
...
}

time series 컬렉션 을 쿼리 하려면 데이터를 검색 하고 집계할 때와 동일한 규칙을 사용합니다.

언급된 작업에 대해 자세히 알아보려면 다음 가이드를 참조하세요.

  • Time Series 컬렉션

  • 시계열 수집 제한 사항

  • 명령 실행

  • Retrieve Data

이 가이드에서 설명하는 메서드나 유형에 대해 자세히 알아보려면 다음 API 설명서를 참조하세요.

돌아가기

GridFS.