node.js - 使用nodejs向DocumentDB中的文档添加新属性

标签 node.js azure azure-cosmosdb

我在 DocumentDB 中有一个 JSON 文档。

我希望向给定文档添加新数据。

例如。

当前文档:

{
    "User": "ABC",
    "UserID": "123"
}

新文档

{
    "User": "ABC",
    "Height":"1.60",
    "UserID": "123"
}

我尝试过此处提到的解决方案:

Add new properties to DocumentDB Document in Stored procedure

但我得到“getContext 未定义”。

我认为问题在于我不确定如何通过函数调用文档。

我想要:

var documentURL = "dbs/'dbname'/colls/'collsname'/docs/'docsname'"
editDocument(documentURL)

function editDocument(document) {
    var collection = getContext().getCollection();
    var response = getContext().getResponse();

    document.Height = "1.60";

    collection.createDocument(collection.getSelfLink(), document, function(err, result) {
    if(err) throw err;

       // Return the resulting document back as the response.
       response.setBody(result);
    });
}

我应该使用 client.replaceDocument 来代替,或者也许有更好的方法来做到这一点。

问题可能是函数显然试图使用函数中的文本位置“dbs/colls/docs”而不是文档本身。

感谢您提前提供的任何帮助!

最佳答案

是的,您需要使用replaceDocument

var documentURL = "dbs/'dbname'/colls/'collsname'/docs/'docid'"

client.readDocument(documentURL, (err, doc) => {
    if(err) return console.log(err);

    doc.Height = "1.60";

    client.replaceDocument(documentUrl, doc, (err, result) => {
        if(err) return console.log(err);
        console.log('replaced document');
    });
});

关于node.js - 使用nodejs向DocumentDB中的文档添加新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47083605/

相关文章:

c# - Azure Web作业失败 : Could not load file or assembly

Azure 事件中心防火墙的空设置允许所有内容连接

c# - 作为字符串的动态属性名称

node.js - 无法使用ungit创建存储库

javascript - 如何使用 Cheerio 填充选项值列表的索引号?

sql-server - 在 ASP.NET MVC 中将 SQL Server 连接到 Azure 数据库

c# - 将 GraphQL 查询映射到 Select() linq 到 sql

azure - 如何检查 Cosmos DB 中是否存在数据库?

node.js - node.js 中的 unescape 替代方案

javascript - 字符串中的 NodeJS v6.9.1 变量不起作用