[英]How do you update sub-document in cosmos db
我是Cosmos Db的新手,想了解如何在文檔集中刪除/ upsert子文檔。
如果我有一份文件:
{ "Id": "1234", "Name": "foo", "Items": [ { "Id": "abcd", "Age": 35, "Claims": [ { "Name": "email", "Value": "foo@bar.com" } ] } ] }
我如何能:
1)將項目添加到文檔中的項目列表中。
2)從Items列表中刪除現有Item
3)將項目置於文檔中的項目列表中
4)向項目列表中的現有項目添加/刪除索賠值?
提前致謝。
目前,沒有辦法檢索修改和更新文檔。
但是, User voice feature request
已於2018年3月5日更新:
正如@Sajeetharan所說,azure cosmos db現在不支持partial updates
。 看起來該團隊正積極致力於此功能。 現在,您可以在stored procedure
更新整個文檔。
示例代碼如下:
function updateSproc(id, update) {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var response = getContext().getResponse();
tryQueryAndUpdate();
function tryQueryAndUpdate(continuation) {
var query = {query: "select * from root r where r.id = @id", parameters: [{name: "@id", value: id}]};
var requestOptions = {continuation: continuation};
var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, documents, responseOptions) {
if (err) throw err;
if (documents.length > 0) {
tryUpdate(documents[0]);
} else {
throw new Error("Document not found.");
}
});
}
function tryUpdate(document) {
var requestOptions = {etag: document._etag};
var fields, i;
fields = Object.keys(update);
for (i = 0; i < fields.length; i++) {
document[fields[i]] = update[fields[i]];
}
var isAccepted = collection.replaceDocument(document._self, document, requestOptions, function (err, updatedDocument, responseOptions) {
if (err) throw err;
response.setBody(updatedDocument);
});
}
但是,Azure Cosmos DB支持MongoDB protocol
。 您可以在Azure官方頁面上確認支持的功能。
因此,支持incremental operations
。 請參閱此鏈接 。
希望它可以幫助你。任何關注,請隨時讓我知道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.