Ben*_*ayo 18 azure azure-cosmosdb
如何从Cosmos DB中删除单个记录?
我可以选择使用SQL语法:
SELECT *
FROM collection1
WHERE (collection1._ts > 0)
Run Code Online (Sandbox Code Playgroud)
果然所有文件(类似于行?)都会被退回
但是,当我尝试删除时,这不起作用
DELETE
FROM collection1
WHERE (collection1._ts > 0)
Run Code Online (Sandbox Code Playgroud)
我如何实现这一目标?
Dav*_*gon 29
DocumentDB API的SQL专门用于查询.也就是说,它只提供SELECT,而不是UPDATE或DELETE.
这些操作完全受支持,但需要REST(或SDK)调用.例如,使用.net,您将调用DeleteDocumentAsync()or ReplaceDocumentAsync(),并且在node.js中,这将是对deleteDocument()或的调用replaceDocument().
在您的特定方案中,您可以运行您SELECT的标识以进行删除,然后进行"删除"调用,每个文档一个(或者,为了提高效率和事务性,将要删除的文档数组传递到存储过程中).
最简单的方法可能是使用Azure 存储资源管理器。连接后,您可以深入到选择的容器,选择一个文档,然后将其删除。您可以在https://gotcosmos.com/tools上找到 Cosmos DB 的其他工具。
要考虑的另一种选择是生存时间(TTL)。您可以将其打开以进行收集,然后设置文档的有效期。这些文档将在到期时自动为您清理。
使用以下代码创建存储过程:
/**
* A Cosmos DB stored procedure that bulk deletes documents for a given query.
* Note: You may need to execute this stored procedure multiple times (depending whether the stored procedure 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 c._self FROM c WHERE c.founded_year = 2008"). Note: For best performance, reduce the # of properties returned per document in the query to only what's required (e.g. prefer SELECT c._self over SELECT * )
* @returns {Object.<number, boolean>} Returns an object with the two properties:
* deleted - contains a count of documents deleted
* continuation - a boolean whether you should execute the stored procedure again (true if there are more documents to delete; false otherwise).
*/
function bulkDeleteStoredProcedure(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)
并使用您的分区键(例如:null)和查询来选择文档(例如:SELECT c._self FROM c 以删除全部)执行它。
基于通过查询资源管理器根据条件从 CosmosDB 中删除文档
| 归档时间: |
|
| 查看次数: |
17184 次 |
| 最近记录: |