本文介绍了使用多个分区键从Azure cosmos DB集合中删除文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过azure门户从azure cosmos DB中删除一些文档.我在容器中编写了一个存储过程,该过程将删除必须删除的数据.但是在执行存储过程时,它将要求提供分区键值.我必须删除具有不同分区键的文档.

I have to delete some documents from azure cosmos DB through azure portal.I wrote a stored procedure in container which will delete the data which has to be deleted.But at the time of execution of stored procedure it will ask for partition key value. I have to delete documents which are having different partition keys.

下面是使用的存储过程.

Below given is the Stored Procedure Used.

function bulkDeleteProcedure(deletedate) {
    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var response = getContext().getResponse();

    var query = "SELECT * FROM c where c._ts = " + deletedate;

    var responseBody = {
        docs_deleted: 0,
        continuation: true
    };

    // Validate input.
    if (!query) throw new Error("The query is undefined or null.");

    fetchData();

    function fetchData(continuation) {
        var requestOptions = {continuation: continuation};

        var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, items, responseOptions) {
            if (err) throw err;
            // response.setBody(items);
            if (items.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(items);
                // response.setBody(items.length + " : inside retrvd docs");
            } else if (responseOptions.continuation) {
                // Else if the query came back empty, but with a continuation token; repeat the query w/ the token.
                fetchData(responseOptions.continuation);
            } else {
                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.
            fetchData();
        }
    }

}

假设我的分区键是车辆类型,并且我的车辆类型值是01到10.根据我的要求,sql查询将返回具有10个不同分区键值的文档.

Suppose my partition key is vehicle type and I am having vehicle type values as 01 to 10. The sql query as per my requirement will return documents with 10 different partition key values.

当前情况就像我必须通过每次提供每个分区键值来运行存储过程10次.是否可以一次运行针对不同分区键值的此存储过程?

Current scenario is like i have to run the stored procedure 10 times by providing each partition key value each time.Is it possible to run this stored procedure for different partition key values in a single go?

推荐答案

不幸的是,没有.您不能为存储过程提供多个分区键.您将需要执行10次存储过程.

Unfortunately no. You can't provide multiple partition keys to a stored procedure. You will need to execute the stored procedure 10 times.

您可以做的一件事是使用任何可用的SDK并编写代码以循环执行存储过程.您可以创建一个分区键数组并循环遍历,并为该数组中的每个分区键执行存储过程.

One thing you could do is use any available SDK and write code to execute stored procedure in a loop. You could create an array of partition keys and loop through that and execute stored procedure for each partition key in that array.

这篇关于使用多个分区键从Azure cosmos DB集合中删除文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 16:42