Eug*_*nio 3 stored-procedures azure azure-cosmosdb
目标是输入一个简单的字符串查询,如
SELECT *
FROM c
WHERE c.deviceId = "device1"
Run Code Online (Sandbox Code Playgroud)
并且需要删除所有由此产生的获取的文档。
我找到了关于使用存储过程执行此操作的非常旧的帖子,但我无法使其与“新”UI 一起正常工作。
非常感谢。
编辑:我觉得@jay-gong 指出了正确的方向,但我在他的解决方案中遇到了问题:
我可以正确创建存储过程,但是当我尝试执行它时,它要求我提供分区键,但在执行后,它不会删除任何文档。
该集合只有几个文档,它的分区键是/message/id我在分区键字段中写的。
Jay*_*ong 10
由于cosmos db不支持SQL删除文档(CosmosDB的Delete 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
此外,据我所知,存储过程有 5 秒的执行限制。如果您遇到超时错误,您可以将继续标记作为参数传递给存储过程并多次执行存储过程。
更新答案:
存储过程中的分区集合需要分区键。(详细解释请参考:Azure Cosmos DB 请求存储过程的分区键。)
所以,首先,上面的代码需要你的分区键。例如,你的分区键定义为/message/id,你的数据如下:
{
"message":{
"id":"1"
}
}
Run Code Online (Sandbox Code Playgroud)
然后你需要将 pk 作为message/1.
很明显,你的查询sql是跨分区的,建议你使用http触发器azure函数代替存储过程。在那个函数中,你可以使用cosmos db sdk代码来做查询和删除操作。不要忘记设置EnableCrossPartitionQuery为true。请参考此案例:Azure Cosmos DB 询问存储过程的分区键。