Jer*_*yal 5 azure azure-cosmosdb
什么是查询或其他快速方法来删除集合中的所有文档?现在我正在删除整个集合并重新创建.
什么是查询或其他快速方法来删除集合中的所有文档?
正如Chris Pietschmann提到Azure门户目前不支持它,Azure Documentdb团队对此功能进行了深入研究.
我们可以使用服务器端脚本(例如存储过程,udfs,触发器)来实现这一点.我从另一个SO线程获得以下代码.它在我身边正常工作.
/**
* A DocumentDB stored procedure that bulk deletes documents for a given query.<br/>
* Note: You may need to execute this sproc multiple times (depending whether the sproc is able to delete every document within the execution timeout limit).
*
* @function
* @param {string} query - A query that provides the documents to be deleted (e.g. "SELECT * FROM c WHERE c.founded_year = 2008")
* @returns {Object.<number, boolean>} Returns an object with the two properties:<br/>
* deleted - contains a count of documents deleted<br/>
* continuation - a boolean whether you should execute the sproc again (true if there are more documents to delete; false otherwise).
*/
function bulkDeleteSproc(query) {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var response = getContext().getResponse();
var responseBody = {
deleted: 0,
continuation: true
};
// 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)
Azure门户网站上的更多详细步骤如下:
小智 5
在测试环境中,我们发现了将TTL设置为1秒,等待cosmosdb进行操作并将TTL恢复为正常值的技巧。
幸运的是,这可以在azure门户中完成。
在后台,Cosmosdb会删除所有文档本身,但这确实需要时间。
示例:如果您在一个集合上有1000个文档,并且ttl关闭
select count(1) from c = 1000
Run Code Online (Sandbox Code Playgroud)
设定TTL = 1秒
select count(1) from c = 0
Run Code Online (Sandbox Code Playgroud)
但是,如果重新打开普通TTL并在有时间删除后台所有文档之前进行计数,则得到的数字与将TTL设置为1秒之前的数字相同。将它们全部在后台删除需要花费时间。
| 归档时间: |
|
| 查看次数: |
8658 次 |
| 最近记录: |