azure - 如何通过查询资源管理器清除DocumentDB中的集合

标签 azure azure-cosmosdb

删除集合中所有文档的查询或其他快速方法是什么?现在我正在删除整个集合并再次重新创建。

最佳答案

What's the query or some other quick way to remove all the documents in a collection?

正如 Chris Pietschmann 提到的,目前 Azure 门户不支持它,并且此 feature由 Azure Documentdb 团队负责。

我们可以使用服务器端脚本(例如存储过程、udfs、触发器)来做到这一点 我从另一个 SO thread 得到以下代码。它在我这边工作正常。

/**
 * A DocumentDB stored procedure that bulk deletes documents for a given query.<br/>
 * Note: You may need to execute this sproc multiple times (depending whether the sproc 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 * FROM c WHERE c.founded_year = 2008")
 * @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 sproc again (true if there are more documents to delete; false otherwise).
 */
function bulkDeleteSproc(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();
        }
    }
}

在Azure门户上执行的更详细步骤如下:

  1. 检查集合中文档的数量

enter image description here

  • 在集合中创建农产品商店
  • enter image description here

  • 检查集合中的所有文档是否已删除
  • enter image description here

    关于azure - 如何通过查询资源管理器清除DocumentDB中的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43587572/

    相关文章:

    Azure AD b2c 删除密码重置中的更改密码步骤

    azure - 无法使用 azure JavaScript 函数和 Key Vault key 检索 cosmosDB 数据

    java - 如何使用 Gremlin 和 Java 查询远程 Apache Tinkerpop 图形数据库?

    azure - 从 Azure DocumentDB 导出数据

    c# - 当您不知道分区键时使用 DocumentClient.ReadDocumentAsync

    azure - 想要创建 Azure 逻辑应用表达式

    azure - 对 Azure Function App 实现 Application Insight

    node.js - Azure linux上的Git推送 Node js失败,kudu想要运行dotnet命令

    powershell - Azure Powershell : Restoring a blob snapshot to a different VM

    Azure CosmosDB 数据触发器不进行实际更新