英文:
inserting into a timeseries collection of a database that has not been created yet
问题
在我的APP中,需要动态地将文档插入到一个可能尚不存在的数据库中的时间序列集合中。
如果我写入一个不存在的集合,mongoDB会创建它,但它不会是一个时间序列。
如何实现这一点?
- 数据库可能尚不存在。
- 因此,时间序列集合也可能尚不存在。
英文:
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?
- The database may not exists yet.
- So of course, the timeseries collection may not exist yet.
答案1
得分: 1
The documentation states the following directly (emphasis added):
>在您能够将数据插入时间序列集合之前,您必须明确地创建该集合,可以使用db.createCollection()
方法或create
命令来执行。
因此,这似乎没有内置在数据库中执行此操作的能力。这表明您有两个选择:
- 添加逻辑来检查集合是否存在,如果需要的话,再插入数据前创建集合。
- 使用一个客户端库/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:
- Add the logic to check for the existence of and, if needed, create the collection before inserting data.
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论