在尚未创建的数据库中插入时间序列集合。

huangapple go评论60阅读模式
英文:

inserting into a timeseries collection of a database that has not been created yet

问题

在我的APP中,需要动态地将文档插入到一个可能尚不存在的数据库中的时间序列集合中。

如果我写入一个不存在的集合,mongoDB会创建它,但它不会是一个时间序列。

如何实现这一点?

  1. 数据库可能尚不存在。
  2. 因此,时间序列集合也可能尚不存在。
英文:

In my APP, there is a need to inert documents on the fly to a timeseries collection in a database that may not exist yet.

If I write a collection that does not exist, mongoDB will create it, but it won't be a timeseries.

How can this be done?

  1. The database may not exists yet.
  2. So of course, the timeseries collection may not exist yet.

答案1

得分: 1

The documentation states the following directly (emphasis added):

>在您能够将数据插入时间序列集合之前,您必须明确地创建该集合,可以使用db.createCollection()方法或create命令来执行。

因此,这似乎没有内置在数据库中执行此操作的能力。这表明您有两个选择:

  1. 添加逻辑来检查集合是否存在,如果需要的话,再插入数据前创建集合。
  2. 使用一个客户端库/ORM,它可以为您执行此操作。

您没有提到使用的是哪种编程语言,
但是关于后一种选择,这个问题 暗示了 Mongoose 是支持这种抽象/功能的一个库。

英文:

The documentation states the following directly (emphasis added):

>Before you can insert data into a time series collection, you must explicitly create the collection using either the db.createCollection() method or the create command.

Therefore it sounds like there is no ability to do this built in to the database itself. This suggests you have two options:

  1. Add the logic to check for the existence of and, if needed, create the collection before inserting data.
  2. Use a client library/ORM that does this for you.

You haven't mentioned which language you are using
but regarding the latter option, this question suggests that Mongoose is one library that supports the abstraction/functionality.

huangapple
  • 本文由 发表于 2023年6月9日 00:36:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定