如何在 cosmos 数据库中编写存储过程来对文档进行更新

How to write a stored procedure in the cosmos database to do an update on a document

我观察到我们无法在 Azure 门户中的查询编辑器上像 SELECT 这样的容器编写更新查询。我想要一个可用于重置我的文档数据的可重用代码。

请告诉我编写存储过程是否是实现此目的的正确方法。

我正在尝试更新文档说,

{ “partitionKey”:“98776632-34er-ii76-318a-9876543456ae”, “顾客”: { "firstName": "UserFirstName", "lastName": "用户姓氏", "email": "user@gmail.com", “密码”:“#4Ftyer%4s” }, “余额”:400 }

Update c 
SET c.balance = 0, c.customer.password="ResetPasswordValue" 
Where c.partitionKey = "98776632-34er-ii76-318a-9876543456ae" 

请告诉我如何使用存储过程获得此工具。谢谢

首先,COSMOSDB目前还不支持对文档进行部分更新,你的测试也证明了这一点。这是一个 highly voted request 并且没有任何更新。

所以我想删除并插入一个新文档。

==================更新======================

我试过这个代码

function example(param) {
    var collection = getContext().getCollection();
    var containerLink = collection.getSelfLink(); 
    var documentQuery ={
                    'query': 'SELECT * FROM c where c.category = @param ',
                    'parameters': [{ 'name': '@param', 'value': param }]
                };
    
    collection.queryDocuments(containerLink, documentQuery,
        function (err, items) {
            console.log(JSON.stringify(items));
            items.forEach(element => {
                element.name="bac";
                collection.replaceDocument(element._self, element, function (err) {
                    if (err) throw err;
                });
            });
        }
    );
}