在存储过程中向 DocumentDB 文档添加新属性

Add new properties to DocumentDB Document in Stored procedure

我有一个 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": []
                ]
}

您可以使用简单的 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);
  });

}