Add new properties to DocumentDB Document in Stored procedure

99封情书 提交于 2019-12-08 04:25:59

问题


I have a JSON document which I am passing to DocumentDB stored procedure. Is there a way I can add more properties to document in store procedure

Passed to DocumentDB:

{
    "id": "Authentication",
    "name": "All users must be authenticated before being authorized for any access to service data",
    "type": "Control"
}

Expected changes in Stored Procedure:

{
    "id": "Authentication",
    "accountId": "Test",
    "versions": [
                 "name": "All users must be authenticated before being authorized for any access to service data",
                 "type": "Control",
                 "tags": [],
                 "links": []
                ]
}

回答1:


You can manipulate the object (add / remove properties) using plain JavaScript syntax.

And then use the DocumentDB server-side JavaScript SDK to create the document.

Here's an example Stored Procedure to get you started:

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);
  });

}


来源:https://stackoverflow.com/questions/31821031/add-new-properties-to-documentdb-document-in-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!