azure - 从 Azure 门户删除 Cosmos 集合中的文档

标签 azure azure-cosmosdb azureportal

运行以下存储过程会导致零条记录被删除。

/**
 * A Cosmos DB stored procedure that bulk deletes documents for a given query.
 * 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:
 *   deleted - contains a count of documents deleted
 *   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
    };
    
    console.log(query);

    // 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;
            
            console.log(retrievedDocs.length);

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

retrievedDocs.length 始终为 0。这就是我调用该过程的方式: enter image description here

查询是SELECT c._self FROM c WHERE c.type = 'ORDER'

我已通过门户手动查询确认上述查询返回结果。

此代码来自this answer .

最佳答案

此存储过程永远无法批量删除记录。

您的容器按/id 分区。这有效地将 Cosmos 转变为一个键值存储,每个分区一个文档。由于存储过程的范围仅限于单个分区,因此该存储过程一次只能有效地删除一条记录,从而无法执行任何批量操作。

对此数据进行批量操作的唯一方法是将其复制到具有不同分区键的新容器。

关于azure - 从 Azure 门户删除 Cosmos 集合中的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69032264/

相关文章:

azure - 删除 CosmosDB 中的 'foreign key' 关系?

azure-cosmosdb - 拥有 Cosmos DB 容器 100% 副本的最简单方法

azure - 如何阻止 Azure 资源组列表始终恢复为对特定订阅进行筛选?

python - 通过 pyodbc 连接到 Azure SQL 数据库

Azure 部署脚本失败并显示 AuthenticationFailed

azure - 不再能够从 GitHub 部署 VirtoCommerce

azure - CosmosDB 具有有限过时一致性的单区域数据库/帐户中的 K 和 T 是什么意思?

azure - 无法使用新门户在 Azure AD 中添加 Microsoft 帐户

azure-active-directory - 在旧的 Azure 门户中找不到订阅

Azure DevOps API WIQL获取最大工作项ID