我在 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/