如何通过C#代码删除DocumentDB中的所有文档 [英] How to delete all the documents in DocumentDB through c# code

查看:98
本文介绍了如何通过C#代码删除DocumentDB中的所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft的一个名为DocumentDB的新数据库.现在,我想按ID删除文档,但是我不知道如何执行此操作. DocumentDB中的删除操作需要自链接,它们与我自己的ID不同. 但是我查询一次文档,然后我将获得自我链接. 通过该自我链接,我正在删除文档.

I'm using a new database from Microsoft called DocumentDB. Now I want to delete a document by ID, but I cannot figure out, how to do this. Delete operation in DocumentDB requires self-links and they are different from my own ids. However I am querying once for document, then I will get the self link. With that self link I am deleting the document.

现在,我要删除集合中50000多个文档中的所有文档.

Now I want to delete all documents around 50000+ documents in my collection.

需要获取每个文档然后删除还是使用任何简单的方法来进行相同操作?

Need to get each document and then delete or any simple method to do the same?

有可能吗?

推荐答案

您是正确的,删除​​文档需要引用文档的_self链接.

You're correct that deleting documents require a reference to the document's _self link.

如果您要删除集合中的 ALL 文档-删除并重新创建集合可能会更简单,更快捷.唯一的警告是服务器端脚本(例如sprocs,udfs,triggers)也属于该集合,并且可能还需要重新创建.

If you are looking to delete ALL documents in your collection - it may be simpler and faster to delete and re-create the collection. The only caveat is that server-side scripts (e.g. sprocs, udfs, triggers) also belong to the collection and may need to be re-created as well.

更新:我编写了一个快速存储的过程,该过程执行了给定查询的批量删除操作.这样,您可以在更少的网络请求中执行批量删除操作.

Update: I wrote a quick stored procedure that performs a bulk-delete given a query. This allows you to perform bulk delete operations in fewer network requests.

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

这篇关于如何通过C#代码删除DocumentDB中的所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆