Add new properties to DocumentDB Document in Store

2019-06-02 07:01发布

问题:

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

}