【发布时间】:2017-11-02 19:54:40
【问题描述】:
我在 DocumentDB 中有一个 JSON 文档。
我希望向给定文档添加新数据。
例如。
当前文档:
{
"User": "ABC",
"UserID": "123"
}
新建文档
{
"User": "ABC",
"Height":"1.60",
"UserID": "123"
}
我已经尝试过这里提到的解决方案:
Add new properties to DocumentDB Document in Stored procedure
但我得到“getContext is not defined”。
我认为问题在于我不确定如何通过函数调用文档。
我想要:
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”而不是文档本身。
提前感谢您的帮助!
【问题讨论】:
-
您需要将存储过程实际上传到 Cosmos 并在那里调用它。 getContext 特定于该运行时环境,您不能只在随机节点脚本中运行它
-
我明白了,有没有不用存储过程编辑文档的方法?
-
否,除非您使用存储过程,否则您只能创建、更新插入或替换整个文档。您将无法修改个别属性
标签: node.js azure azure-cosmosdb