【问题标题】:How to clear the collection in DocumentDB through Query Explorer如何通过查询资源管理器清除 DocumentDB 中的集合
【发布时间】:2017-04-24 12:03:10
【问题描述】:

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

【问题讨论】:

  • AFAIK,没有其他方法可以从门户中做到这一点。
  • 如果有用,请将其标记为答案,以帮助更多有问题的社区。​​span>
  • 你的答案将帮助用户,这就是我支持它的原因,但 1)你的答案不符合快速捷径 2)我希望有一些官方的方法来做到这一点(这仍在进度)...所以我会一直打开到时间
  • 目前,Azure 门户不支持它。而这个feature 正在由 Azure Documentdb 团队进行审查

标签: azure azure-cosmosdb


【解决方案1】:

删除集合中所有文档的查询或其他快速方法是什么?

正如 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. 检查集合中的文档数

  1. 在集合中创建一个农产品商店

  1. 检查集合中的所有文档是否都已删除

【讨论】:

【解决方案2】:

在测试环境中,我们发现了将 TTL 设置为 1 秒的技巧,等待 cosmosdb 完成它的工作,然后将 TTL 恢复正常。

很高兴这可以在 azure 门户中完成。

在后台,Cosmosdb 会自行删除所有文档,但确实需要时间。

示例:如果您的集合中有 1000 个文档并且 ttl 已关闭

select count(1) from c = 1000

设置 TTL = 1 秒

select count(1) from c = 0

但是如果您重新打开正常 TTL 并在它有时间删除后台中的所有文档之前进行计数,您会得到与将 TTL 设置为 1 秒之前相同的数字。 在后台将它们全部删除需要时间。

【讨论】:

    【解决方案3】:

    进行返回 selflink + 分区键(此处= company.id)的查询,然后删除每个文档

    protected async Task DeleteAllDocumentsAsync()
        {
            DocumentClient client = createClient();
            //**make query that returns selflink + partition key (here= company.id)**
            var docs = client.CreateDocumentQuery("your collection uri", "select c._self, c.company.id from c", new FeedOptions() {EnableCrossPartitionQuery = true}).ToList();
    
            foreach (var doc in docs)
            {
                var requestOptions = new RequestOptions() {PartitionKey = new PartitionKey(doc.id)};
                await client.DeleteDocumentAsync(doc._self, requestOptions);
            }
        }
    

    【讨论】:

      【解决方案4】:

      在 Azure 门户中找到了一个 hack。

      • 在容器设置下,打开生存时间并将时间设置为一个较小的数字(10 秒)。
      • 单击保存。项目将是 deleted in the background 并且需要一些时间(取决于容器中的数据量)。您可以密切关注Data Size field under metrics 以确保所有数据均已删除。
      • 关掉生存时间。

      【讨论】:

        【解决方案5】:

        目前,您正在执行的方法是通过 Azure 门户执行此操作的唯一方法。您可能希望考虑使用脚本或编码来实现一个工具来为您删除集合,而不是删除集合来完成它。

        【讨论】:

        • 保存和执行按钮不起作用。它是灰色的,只有“保存”选项。这很愚蠢。
        猜你喜欢
        • 1970-01-01
        • 2017-11-30
        • 2020-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-30
        相关资源
        最近更新 更多