【问题标题】:Add new properties to DocumentDB Document in Stored procedure在存储过程中向 DocumentDB Document 添加新属性
【发布时间】:2015-08-04 23:27:10
【问题描述】:

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

【问题讨论】:

    标签: properties add azure-cosmosdb


    【解决方案1】:

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

    【讨论】:

    • 这解决了我的问题,这就是我想要的。谢谢!!
    • 对于未来的读者:调用 Collection.replaceDocument 时,简单地向现有文档添加新对象属性不起作用。到目前为止,我仍然不知道如何在存储过程中将属性添加到现有文档而不删除/重新创建它
    猜你喜欢
    • 1970-01-01
    • 2015-11-20
    • 2012-02-13
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多