通过门户从 Azure Cosmos DB 删除所有/多个文档

2024-01-02

是否可以通过 azure 门户、Azure cosmos SQL 查询或 power shell 脚本删除集合中的所有/多个可用文档?


根据我的经验,删除所有文档的最快方法是将容器上的“生存时间”设置为 1 秒。这将删除所有文档。但请注意,此过程需要一些时间,因此如果您过早将“生存时间”设置回无限制,尚未删除的文档将会重新出现。

您可以在容器的“规模和设置”下设置“生存时间”:url ->https://learn.microsoft.com/en-us/azure/cosmos-db/how-to-time-to-live https://learn.microsoft.com/en-us/azure/cosmos-db/how-to-time-to-live

您还可以在容器中创建一个存储过程并运行该..url ->https://github.com/Azure/azure-cosmosdb-js-server/blob/master/samples/stored-procedures/bulkDelete.js https://github.com/Azure/azure-cosmosdb-js-server/blob/master/samples/stored-procedures/bulkDelete.js

存储过程:

/**
 * A Cosmos DB stored procedure that bulk deletes documents for a given query.<br/>
 * Note: You may need to execute this stored procedure multiple times (depending whether the stored procedure is able to delete every document within the execution timeout limit).
 *
 * @function
 * @param {string} query - A query that provides the documents to be deleted (e.g. "SELECT c._self FROM c WHERE c.founded_year = 2008"). Note: For best performance, reduce the # of properties returned per document in the query to only what's required (e.g. prefer SELECT c._self over SELECT * )
 * @returns {Object.<number, boolean>} Returns an object with the two properties:<br/>
 *   deleted - contains a count of documents deleted<br/>
 *   continuation - a boolean whether you should execute the stored procedure again (true if there are more documents to delete; false otherwise).
 */
function bulkDeleteStoredProcedure(query) {
    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var response = getContext().getResponse();
    var responseBody = {
        deleted: 0,
        continuation: true
    };

    // Validate input.
    if (!query) throw new Error("The query is undefined or null.");

    tryQueryAndDelete();

    // Recursively runs the query w/ support for continuation tokens.
    // Calls tryDelete(documents) as soon as the query returns documents.
    function tryQueryAndDelete(continuation) {
        var requestOptions = {continuation: continuation};

        var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, retrievedDocs, responseOptions) {
            if (err) throw err;

            if (retrievedDocs.length > 0) {
                // Begin deleting documents as soon as documents are returned form the query results.
                // tryDelete() resumes querying after deleting; no need to page through continuation tokens.
                //  - this is to prioritize writes over reads given timeout constraints.
                tryDelete(retrievedDocs);
            } else if (responseOptions.continuation) {
                // Else if the query came back empty, but with a continuation token; repeat the query w/ the token.
                tryQueryAndDelete(responseOptions.continuation);
            } else {
                // Else if there are no more documents and no continuation token - we are finished deleting documents.
                responseBody.continuation = false;
                response.setBody(responseBody);
            }
        });

        // If we hit execution bounds - return continuation: true.
        if (!isAccepted) {
            response.setBody(responseBody);
        }
    }

    // Recursively deletes documents passed in as an array argument.
    // Attempts to query for more on empty array.
    function tryDelete(documents) {
        if (documents.length > 0) {
            // Delete the first document in the array.
            var isAccepted = collection.deleteDocument(documents[0]._self, {}, function (err, responseOptions) {
                if (err) throw err;

                responseBody.deleted++;
                documents.shift();
                // Delete the next document in the array.
                tryDelete(documents);
            });

            // If we hit execution bounds - return continuation: true.
            if (!isAccepted) {
                response.setBody(responseBody);
            }
        } else {
            // If the document array is empty, query for more documents.
            tryQueryAndDelete();
        }
    }
}

然后,您可以编写一个 powershell 脚本来运行该存储过程。

UPDATE

我相信设置“生存时间”的另一个优点是它不会花费任何 RU,但使用存储过程删除会花费任何 RU。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过门户从 Azure Cosmos DB 删除所有/多个文档 的相关文章

随机推荐

  • 将样式应用于保存到 HTML 文件的 Pandas 数据框

    我在 Jupyter IPython 笔记本中有一个 Pandas 数据框 Jupyter 内的数据框作为 HTML 表格的样式非常好 标题行采用粗体样式 字体美观 表格边框较细 然后我将数据框导出到 HTML 文件 按照说明here ht
  • axios:如何一个接一个地运行多个请求?

    我有一个非常大的 ID 数组 数千个 ID 我想循环遍历这个数组 并对每个值向 API 发出请求 如下所示 12 32 657 1 67 forEach id gt axios get myapi com user id then data
  • 使用 GitHub Actions 从私有 GitHub 存储库安装 npm 模块

    我正在尝试使用 GitHub Actions 运行 Node js 项目的构建 作为npm install 我需要直接从私有 GitHub 存储库 而不是从 GPR 安装 npm 模块 In the package json I have
  • Java 8 Stream 函数将字谜列表分组为列表映射

    Java 8 即将发布 在学习 Streams 时 我遇到了一个使用其中一种新方法对字谜进行分组的场景 我面临的问题是我找不到使用 map reduce 函数对 Strings 对象进行分组的方法 相反 我必须创建与记录类似的方法聚合运算
  • Xcode -- 让force_load 使用相对路径

    某些库在链接到 Xcode 项目时需要 all load 链接器标志 但是 如果库之间存在符号冲突 这会导致链接器错误 解决方案是使用 force load 它可以有效地让您在某些库上使用 all load 但不能在其他库上使用 然而 这反
  • 如何仅使用 CSS 制作圆形面包屑

    有谁见过可用的 css3 解决方案来实现上述内容吗 我想找到一种不需要图像的方法 也不重写其他人可能已经很好解决的问题 有人知道现有的解决方案吗 使用起来相当简单CSS3 s border radius财产 现场演示 http jsbin
  • 重命名数据库中的所有表

    我有一个数据库 其中所有表都以一组相同的字符为前缀 这样做是因为它们曾经位于一个为宠物项目设置的共享数据库中 其中包含数百个其他表 应用程序以及数据库现在已准备好脱离该阶段并准备独立运行 我想删除每个表的前缀 有没有比右键单击并单独重命名每
  • 更改 nginx 上的标头“Django 管理”文本

    我跟着这个问题 https stackoverflow com questions 4938491 django admin change header django administration text更改我的 django 管理面板标
  • 根据表格数据找到每个部门的第三个最高工资

    我需要找出3rd maximum salary对于一个员工 对于一个部门的每个部门table if no 3rd maximum salary存在然后显示2nd maximum salary if no 2nd maximum salary
  • 如何从电子邮件地址获取 SMTP 服务器

    我想通过特殊的电子邮件帐户自动发送邮件 但现在 我只知道电子邮件地址 电子邮件受保护 cdn cgi l email protection和密码 那么你知道如何获取 SMTP 服务器吗 下面是我的 C 代码 SmtpClient clien
  • Perl:何时释放不需要的标量内存而不超出范围?

    我有一个应用程序 可以将大量文本数据读取为标量 有时甚至是 GB 大小 我用substr在该标量上将大部分数据读取到另一个标量中 并将提取的数据替换为空字符串 因为第一个标量中不再需要它 我最近发现 Perl 没有释放第一个标量的内存 但它
  • 单击时,循环遍历每个对象键

    我还在学习 JS 有些东西比其他人更难理解 就像这样 我试图通过允许用户单击自定义按钮来更改谷歌地图的主题 我正在使用 if else 效果很好 但我想添加更多主题并使用循环 用户每次单击时 都会选择 object key 0 then c
  • 从 C++ 调用 DLL 中的函数

    我在 VS 2008 中有一个解决方案 其中有 2 个项目 一个是用 C 编写的 DLL 另一个是从空白项目创建的简单 C 控制台应用程序 我想知道如何从应用程序调用 DLL 中的函数 假设我从一个空白的 C 项目开始 并且我想调用一个名为
  • 打印所有定义的变量和值

    这只是为我提供了已定义变量的数组 但没有打印出任何变量 我怎样才能打印值 另外我可以使用什么函数以这种格式输出所有定义的变量 变量名称 变量类型 int array string bool 在线定义变量 脚本中定义的变量 使用次数可变 变量
  • 了解并发.futures.Executor.map()

    我正在尝试使用进程并行化一些Python代码concurrent futures https docs python org 3 library concurrent futures html 看起来我可以通过以下方式多次并行执行一个函数提
  • Windows 服务启动/停止另一个服务

    我用 C 编写的服务有一个小问题 该服务本身运行正常并且在 SYSTEM 帐户下运行 在一个执行点期间 我必须启动或停止另一项服务 然而 这是行不通的 致电给OpenService 返回错误代码 5 即 访问被拒绝 提供更多细节 我必须启动
  • 列表到字典

    我有一个List
  • 如何在 SQL Server 中选择连续重复项

    我想从 SQL Server 表中选择重复的条目 但前提是 id 是连续的 我一直在努力扭转这个答案 https stackoverflow com a 3298645 1778169满足我的需要 但我无法让它工作 上面的答案是针对Orac
  • AudioKit - 如何从麦克风获取实时 floatChannelData?

    我是 Audiokit 的新手 我正在尝试对来自麦克风的输入音频进行一些实时数字信号处理 我知道我想要的数据在AKAudioFile的FloatChannelData中 但是如果我想实时获取这个数据怎么办 我目前正在使用 AKMicroph
  • 通过门户从 Azure Cosmos DB 删除所有/多个文档

    是否可以通过 azure 门户 Azure cosmos SQL 查询或 power shell 脚本删除集合中的所有 多个可用文档 根据我的经验 删除所有文档的最快方法是将容器上的 生存时间 设置为 1 秒 这将删除所有文档 但请注意 此