在存储过程中向DocumentDB文档添加新属性 [英] Add new properties to DocumentDB Document in Stored procedure

查看:91
本文介绍了在存储过程中向DocumentDB文档添加新属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON文档,该文档将传递给DocumentDB存储过程。有没有一种方法可以在存储过程中向文档添加更多属性

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

传递给DocumentDB:

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": []
                ]
}


推荐答案

您可以使用普通的JavaScript语法操作对象(添加/删除属性)。

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

然后在服务器端使用DocumentDB JavaScript SDK创建文档

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

}

这篇关于在存储过程中向DocumentDB文档添加新属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆