当前位置: 首页 > 知识库问答 >
问题:

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

吕皓
2023-03-14

是否可以通过 Azure 门户、Azure Cosmos SQL 查询或电源外壳脚本删除集合中可用的所有/多个文档?

共有1个答案

郑晨
2023-03-14

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

您可以在容器的“比例和设置”下设置“生存时间”: url -

还可以在容器中创建一个存储过程并运行该存储过程。网址 -

存储过程

/**
 * 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();
        }
    }
}

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

更新

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

 类似资料:
  • 问题内容: $(“#topNav” + $(“#breadCrumb2nd”).text().replace(” “, “”)).addClass(“current”); 这是我的代码的一部分。我想在获取另一个ID的text属性后向ID添加一个类。问题在于,持有我需要的文本的ID包含字母之间的空格。 我希望删除空白。我已经尝试过,但这只能部分起作用。该只删除第一个空间。 问题答案: 您必须告诉re

  • 问题内容: 我试图根据另一个数据库的选择标准从一个数据库中删除记录。我们有两个表,emailNotification,用于存储作业和电子邮件列表。然后我们有工作。我想清除已关闭作业的emailNotifications。我在Stackoverflow上找到了一些较早的示例,这些示例将我带到了这种语法类型(我以前曾尝试在where之前进行连接)。 我收到错误消息,您无法在FROM子句中指定目标表’e

  • 问题内容: 有没有办法从所有值的特定列中删除所有空格? 问题答案: 替换: 删除所有字符: 删除所有字符: http://dev.mysql.com/doc/refman/5.0/zh-CN/string- functions.html#function_replace 删除列: http://dev.mysql.com/doc/refman/5.0/zh-CN/string- functions

  • 问题内容: 我刚刚在android应用程序中编写了一个函数,该函数使用Java中的标准“文件”类删除文件。即: 尽管上面的过程很简单,但我一直想知道通过“ ContentResolver”执行该操作是否有任何好处。任何意见,将不胜感激。 干杯, 杰瑞德 ------------------------------------------编辑------- 这是通过Content Resolver删

  • 问题内容: 我正在尝试使用SQL从连接在一起的多个表中删除多行。 表A连接到表B表B连接到表C 我想删除表B和C中与表A中的一行相对应的所有行 问题答案: 好吧,如果您使用过InnoDB表,则可以使用外键设置级联删除,这将自动完成所有操作。但是,如果您出于某种原因使用MyISAM,则只需使用多表DELETE即可:

  • 问题内容: 我有一个数组: 我想从中删除所有零,以便返回(保持相同的顺序): 有什么比以下方法更简单的方法来删除所有零? 我无法在Arrays类中找到任何方法,并且Google / SO搜索没有给我任何好的答案。 问题答案: 这是在少数情况下,用代码显示比用普通英语解释更容易的情况之一: