我必须通过azure门户从azure cosmos DB中删除一些文档。 我在容器中编写了一个存储过程,该过程将删除必须删除的数据。 但是在执行存储过程时,它将要求分区键值。我必须删除具有不同分区键的文档。
下面是使用的存储过程。
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个不同分区键值的文档。
当前情况就像我必须通过每次提供每个分区键值来运行存储过程10次。 是否可以一次就对不同的分区键值运行此存储过程?
答案 0 :(得分:1)
当前情况就像我必须运行存储过程10次 通过每次提供每个分区键值。是否可以运行 单个存储区中用于不同分区键值的此存储过程 去吗?
不幸的是,没有。您不能为存储过程提供多个分区键。您将需要执行10次存储过程。
您可以做的一件事是使用任何可用的SDK并编写代码以循环执行存储过程。您可以创建一个分区键数组并循环遍历,并对该数组中的每个分区键执行存储过程。