[英]Delete all/multiple documents from Azure Cosmos DB through the portal
[英]Delete documents from Azure cosmos DB collection with multiple Partition Keys
我必須通過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次。 是否可以一次就對不同的分區鍵值運行此存儲過程?
當前情況就像我必須通過每次提供每個分區鍵值來運行存儲過程10次。 是否可以一次就對不同的分區鍵值運行此存儲過程?
抱歉不行。 您不能為存儲過程提供多個分區鍵。 您將需要執行10次存儲過程。
您可以做的一件事是使用任何可用的SDK並編寫代碼以循環執行存儲過程。 您可以創建一個分區鍵數組並循環遍歷,並為該數組中的每個分區鍵執行存儲過程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.