【问题标题】:Pre-create collection in Azure Cosmos DB在 Azure Cosmos DB 中预创建集合
【发布时间】:2021-12-01 04:32:12
【问题描述】:

有没有办法在 Azure Cosmos DB 中预先播种或预先创建我需要的集合?在 Azure 上创建资源后,我得到了一个空数据库,但我的目标是在部署后呈现我所需的集合。

我说得对吗,我需要在 Azure DevOps 中将 Azure Cosmos DB 部署到我的 api 部署管道中吗?

感谢您的帮助。

【问题讨论】:

  • 您是如何创建 Cosmos DB 帐户的?请编辑您的问题并提供详细信息。

标签: azure azure-devops azure-cosmosdb azure-cosmosdb-mongoapi


【解决方案1】:

您可以使用 ARM/Bicep 模板创建 cosmosdb 帐户,然后尝试使用您的自定义逻辑播种数据。

我有一个same 的示例设置。

async function populateDb(data, collectionName) {
  MongoClient.connect(
    endpoint,
    {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      sslValidate: false,
    },
    (error, client) => {
      if (error) {
        throw error;
      } 
      database = client.db("yourdb");
      collection = database.collection(collectionName);
      let batch = collection.initializeOrderedBulkOp();

      for (let i = 0; i <= data.length; i += 1) { 
        if (data[i]) {
          batch.insert(data[i]);
        }
      }
      batch.execute().then(() => console.log("Seeding completed on " + collectionName + " Collection", new Date().toLocaleString()))
        .c 
        });
    });
};

【讨论】:

    猜你喜欢
    • 2020-12-22
    • 1970-01-01
    • 2018-06-02
    • 2018-10-12
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多