Azure CosmosDB:存储过程基于查询删除文档

Azure CosmosDB:存储过程基于查询删除文档,azure,stored-procedures,azure-cosmosdb,Azure,Stored Procedures,Azure Cosmosdb,目标是输入一个简单的字符串查询,如 SELECT * FROM c WHERE c.deviceId = "device1" 需要删除所有获取的文档 我找到了一些关于使用存储过程执行此操作的非常老的帖子,但我无法让它在“新”UI中正常工作 先谢谢你 编辑:我觉得@jay gong指向了正确的方向,但他的解决方案遇到了问题: 我可以正确地创建存储过程,但当我尝试执行它时,它会要求提供分区键,我给出了分区键,但在执行之后,它不会删除任何文档 集合只有几个文档,它的分区键是/message/id

目标是输入一个简单的字符串查询,如

SELECT * 
FROM c 
WHERE c.deviceId = "device1"
需要删除所有获取的文档

我找到了一些关于使用存储过程执行此操作的非常老的帖子,但我无法让它在“新”UI中正常工作

先谢谢你

编辑:我觉得@jay gong指向了正确的方向,但他的解决方案遇到了问题:

我可以正确地创建存储过程,但当我尝试执行它时,它会要求提供分区键,我给出了分区键,但在执行之后,它不会删除任何文档


集合只有几个文档,它的分区键是
/message/id
,这是我在分区键字段中写的。

由于cosmos db不支持通过SQL()删除文档,您可以查询文档并通过delete SDK逐个删除它们。也可以在存储过程中选择批量操作

您可以完全按照存储过程批量删除来实现您的需求,这对我来说是可行的

function bulkDeleteProcedure(query) {
    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var response = getContext().getResponse();
    var responseBody = {
        deleted: 0,
        continuation: true
    };

    query = 'SELECT * FROM c WHERE c.deviceId="device1"';

    // 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();
        }
    }
}
此外,据我所知,存储过程有5秒的执行限制。如果遇到超时错误,可以将延续令牌作为参数传递到存储过程中,并多次执行存储过程


更新答案:

存储过程中的分区集合需要分区键。(请参阅详细说明:)

因此,首先,上面的代码需要您的分区密钥。例如,您的分区密钥定义为/message/id,您的数据如下:

{
    "message":{
        "id":"1"
    }
}
然后您需要将pk作为
消息/1
传递


显然,您的查询sql是跨分区的,我建议您采用而不是存储过程。在该函数中,您可以使用cosmos db sdk代码执行查询和删除操作。不要忘记将
EnableCrossPartitionQuery
设置为
true
。请参考此案例:。

谢谢您的详细回答,我尝试了您的解决方案,但没有成功。当我按下Execute键时,它要求输入分区键值。我应该填写这个字段吗?如果是,用哪个参数?我尝试了与我正在其中执行函数的集合相关的分区键,但它似乎不起作用。我的分区键类似于
/message/id
。非常感谢您的详细解释!我以前使用过azure函数,我认为我可以用一种更简单的方法来解决问题,但这似乎是唯一的方法。再次感谢你的帮助@Eugenio不客气,我认为通过查询sql删除文档是cosmos db需要遵循的非常紧急的功能。您可以向cosmos db团队提交反馈。祝您愉快。