Stored procedures DocumentDB从另一个存储过程或自身调用存储过程

Stored procedures DocumentDB从另一个存储过程或自身调用存储过程,stored-procedures,recursion,azure-cosmosdb,Stored Procedures,Recursion,Azure Cosmosdb,有没有一种方法可以对DocumentDB文档递归调用存储过程(或者甚至UDF,如果可以的话) 我们有一个类似以下内容的文档: { "docID" : "my_id", "owner" : "fred", "items" : [ { "itemID" : "1", "type" : "item", "value" : 3 }, { "itemID" : "2", "type" : "group",

有没有一种方法可以对DocumentDB文档递归调用存储过程(或者甚至UDF,如果可以的话)

我们有一个类似以下内容的文档:

{
  "docID" : "my_id",
  "owner" : "fred",
  "items" : [
    {
      "itemID" : "1",
      "type" : "item",
      "value" : 3
    },
    {
      "itemID" : "2",
      "type" : "group",
      "items" : [
        {
          "itemID" : "2.1",
          "type" : "group",
          "items" : [
            {
              "itemID" : "2.1.1",
              "type" : "item",
              "value" : 2
            },
            {
              "itemID" : "2.1.2",
              "type" : "item",
              "value" : 4
            }
          ]
        },
        {
          "itemID" : "2.2",
          "type" : "item",
          "value" : 1
        }
      ]
    }
  ]
}
function sumValues(items) {
    int total = 0;
    forEach(item in items) {
        if (item.type == "item") {
            total += item.value;
        } else {
            total += sumValues(item.items);
        }
    }
    return total;
}

function sumAllValues() {
  var ctx = getContext();
  var coll = ctx.getCollection();
  var response = ctx.getResponse();

  // query for docs by owner
  var filterQuery = 'SELECT * FROM Docs d where d.owner = \\\"fred\\\"';
  var done = coll.queryDocuments(coll.getSelfLink(), filterQuery, {},
    function (err, docs, options) {
      if (err) throw new Error ('Error' + err.message);

      var total = 0;
      docs.forEach(function(doc) {
        total += sumTotals(doc.items);
      });

      response.setBody('Total: ' + total);
    });
}
任何时候我们有
“items”
“items”
数组可以包含混合了
“type”:“item”
“type”:“group”
“类型”的条目:“项”
有一个简单的
“值”
字段,需要求和。
“类型”的条目:“组”
有一个
“项”
数组。。。等等理论上,递归的级别没有限制,我承认这是一个问题,但在实践中,级别很少会低于4或5级

我试图编写的伪代码如下所示:

{
  "docID" : "my_id",
  "owner" : "fred",
  "items" : [
    {
      "itemID" : "1",
      "type" : "item",
      "value" : 3
    },
    {
      "itemID" : "2",
      "type" : "group",
      "items" : [
        {
          "itemID" : "2.1",
          "type" : "group",
          "items" : [
            {
              "itemID" : "2.1.1",
              "type" : "item",
              "value" : 2
            },
            {
              "itemID" : "2.1.2",
              "type" : "item",
              "value" : 4
            }
          ]
        },
        {
          "itemID" : "2.2",
          "type" : "item",
          "value" : 1
        }
      ]
    }
  ]
}
function sumValues(items) {
    int total = 0;
    forEach(item in items) {
        if (item.type == "item") {
            total += item.value;
        } else {
            total += sumValues(item.items);
        }
    }
    return total;
}

function sumAllValues() {
  var ctx = getContext();
  var coll = ctx.getCollection();
  var response = ctx.getResponse();

  // query for docs by owner
  var filterQuery = 'SELECT * FROM Docs d where d.owner = \\\"fred\\\"';
  var done = coll.queryDocuments(coll.getSelfLink(), filterQuery, {},
    function (err, docs, options) {
      if (err) throw new Error ('Error' + err.message);

      var total = 0;
      docs.forEach(function(doc) {
        total += sumTotals(doc.items);
      });

      response.setBody('Total: ' + total);
    });
}
这可能吗?DocumentDB是否支持从另一个存储过程调用存储过程?存储过程可以自己调用吗

我在网上找到了一些DocumentDB存储过程参考,包括和许多其他页面

如果可能的话,我想我可能必须以某种方式查询集合以获取我要调用的存储过程,然后以某种方式引用存储过程,而不是像使用独立语言那样直接调用
sumTotals()


我们刚刚开始研究使用DocumentDB编程,所以我们还不能完全确定我们可以用它做什么。谢谢你的帮助和建议。

我认为你在这方面做得对

不可能从存储过程中执行存储过程

但是,您可以在存储过程中定义JS函数,这些函数可以在存储过程中被引用、调用和重用

在这种情况下,只需在父
sumAllValues()
存储过程中定义
sumValues()
函数(就像您提到的
swapItems()
示例一样)


您还可以为要在多个存储过程和查询中共享和重复使用的逻辑定义UDF。

我可能可以使用一些示例以外的内容来定义直接从父函数调用的函数
swapItems
。投票添加此选项该建议--移动sumValues函数定义在存储过程定义中——对我有用,所以我用代码示例更新了您的答案,并将接受它。