我有一个JSON文档,我将其传递给DocumentDB存储过程。有没有办法可以在商店过程中为文档添加更多属性
传递给DocumentDB:
{
"id": "Authentication",
"name": "All users must be authenticated before being authorized for any access to service data",
"type": "Control"
}
存储过程的预期更改:
{
"id": "Authentication",
"accountId": "Test",
"versions": [
"name": "All users must be authenticated before being authorized for any access to service data",
"type": "Control",
"tags": [],
"links": []
]
}
答案 0 :(得分:3)
您可以使用纯JavaScript语法操作对象(添加/删除属性)。
然后使用DocumentDB服务器端JavaScript SDK创建文档。
这是一个让您入门的示例存储过程:
function transform(doc) {
var collection = getContext().getCollection();
var response = getContext().getResponse();
// Add new accountId and versions fields.
doc.accountId = "Test";
doc.versions = {
name: doc.name,
type: doc.type,
tags: [],
links: []
};
// Remove old name and type fields.
delete doc.name;
delete doc.type;
// Create the document.
collection.createDocument(collection.getSelfLink(), doc, function(err, result) {
if(err) throw err;
// Return the resulting document back as the response.
response.setBody(result);
});
}