删除集合中所有文档的查询或其他快速方法是什么?现在我正在删除整个集合并重新创建。
答案 0 :(得分:5)
查询或其他一些快速删除集合中所有文档的方法是什么?
正如Chris Pietschmann所说,目前Azure门户网站上不支持它,而且Azure {{p>
正在对此feature进行评估。我们可以使用服务器端脚本(例如存储过程,udfs,触发器)来做到这一点 我从另一个SO thread获得以下代码。它在我身边正常工作。
/**
* 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();
}
}
}
Azure门户网站上的更多详细步骤如下:
答案 1 :(得分:2)
在测试环境中,我们发现了将TTL设置为1秒,等待cosmosdb完成其工作,然后将TTL恢复为正常状态的技巧。
幸运的是,这可以在Azure门户中完成。
在后台,Cosmosdb会删除所有文档本身,但这确实需要时间。
示例:如果集合中有1000个文档,并且ttl已关闭
select count(1) from c = 1000
设置TTL = 1秒
select count(1) from c = 0
但是,如果重新打开普通TTL并在其有时间删除背景中的所有文档之前进行计数,则得到的数字与将TTL设置为1秒之前的数字相同。 将它们全部删除在后台需要花费时间。
答案 2 :(得分:1)
使查询返回自链接+分区键(此处= company.id),然后删除每个文档
protected async Task DeleteAllDocumentsAsync()
{
DocumentClient client = createClient();
//**make query that returns selflink + partition key (here= company.id)**
var docs = client.CreateDocumentQuery("your collection uri", "select c._self, c.company.id from c", new FeedOptions() {EnableCrossPartitionQuery = true}).ToList();
foreach (var doc in docs)
{
var requestOptions = new RequestOptions() {PartitionKey = new PartitionKey(doc.id)};
await client.DeleteDocumentAsync(doc._self, requestOptions);
}
}
答案 3 :(得分:0)
目前,您正在执行的方法是通过Azure门户执行此操作的唯一方法。您可能希望使用脚本或编码来实现一个工具来为您删除集合,而不是删除集合来完成它。
答案 4 :(得分:0)
找到了从Azure门户执行此操作的黑客。