文档菜单
文档首页
/ / /
Go 驱动程序
/

时间序列集合

本页内容

  • 概述
  • 创建时间序列集合
  • 查询时间序列集合
  • 附加信息

在本指南中,您可以了解MongoDB中的时间序列集合,以及如何使用MongoDB Go Driver与之交互。

时间序列集合有效地存储在一定时间内测量的序列。该集合由以下信息组成的时间序列数据

  • 随时间收集的数据

  • 描述测量的元数据

  • 测量的日期

示例
测量
元数据
销售数据
收入
公司
感染率
感染者人数
位置

重要

时间序列集合需要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)

为了检查你是否创建了集合,向RunCommand()方法发送"listCollections"命令。

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

要查询时序集合,使用与检索和聚合数据相同的约定。检索和聚合数据。

有关提到的操作的更多信息,请参阅以下指南

  • 时间序列集合

  • 时序集合限制

  • 运行命令

  • 检索数据

要了解更多关于本指南中讨论的任何方法或类型的信息,请参阅以下API文档

返回

GridFS