Docs 菜单
Docs 主页
/
MongoDB for VS Code
/ /

使用VS Code创建时间序列集合

在此页面上

  • 先决条件
  • 创建时间序列集合
  • 例子

您可以使用 MongoDB Playground 创建时间序列集合

如果尚未执行此操作,则必须先完成以下先决条件,然后才能使用 MongoDB Playground 创建 time-series collection:

  • 创建与 MongoDB 部署的连接。

  • 激活与 MongoDB 部署的连接。

使用 MongoDB for VS Code 连接到部署后,请使用左侧导航:

  1. 展开一个活动连接,并将鼠标悬停在您希望collection存在的数据库上。

  2. 单击出现的图标。

  3. MongoDB Playground 会自动打开,并显示模板表单,用于创建常规集合和时间序列集合。

  4. 删除常规collection表单并取消注释time-series表单。

  5. 在提供的字段中填写时间序列集合的详细信息。

  6. 要运行 Playground,请单击 VS Code 导航栏右上角的 Play Button

使用时间序列集合运行 Playground 后,左侧导航将更新集合图标以标识它是时间序列集合。

显示 VS Code 扩展中的time-series图标的图像
点击放大

提示

另请参阅:

此示例在test数据库中创建一个名为 weather的time-series collection。

要使用此示例,请从 MongoDB Playgrounds 中的 collection 模板开始。删除常规集合的模板表单,并保留在常规集合模板下方找到的时间序列集合模板。

use('test');
db.createCollection(
"weather",
{
timeseries: {
timeField: "timestamp",
granularity: "hours",
bucketMaxSpanSeconds: 60,
bucketRoundingSeconds: 60
}
}
)

在示例中:

  • use('test') 选择要添加collection的数据库。

  • timeseries 指定用于创建时间序列集合的字段。

    • timeField: "timestamp" 为time-series文档中包含日期的字段命名。在本例中,为timestamp

    • granularity: "hours" 定义存储文档的时间范围。

    • bucketMaxSpanSeconds 为每个存储桶定义了 60 秒的最大时间跨度。

    • bucketRoundingSeconds 指定确定新存储桶的起始时间戳的时间间隔。

当您按Play Button时,MongoDB for VS Code 会分割 Playground 并在 Playground Results.JSON 窗格中输出以下结果,以确认时间序列集合的创建。

{
"ok": 1
}

weather集合也会显示在集合列表中,并标有时间序列图标。

后退

创建数据库和集合