Is there a way to call stored procedures (or even UDF, if they work) recursively with DocumentDB documents?
We have a document that looks something like this:
{ "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 } ] } ] }
Each time we have "items" , the "items" array can contain entries that are a combination of "type" : "item" and "type" : "group" . Entries that have a value of "type" : "item" have a simple "value" field that needs to be summarized. Entries with "type" : "group" have an array of "items" ... and so on. Theoretically, there is no limit to the level of recursion, which I recognize as a problem, but in practice the levels will rarely be below 4 or 5 depths.
The pseudocode for what I'm trying to write looks something like this:
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); }); }
Is it possible? Does DocumentDB support calling sproc from another sproc? Can sproc call itself?
I found several links to DocumentDB stored procedures on the Internet, including this and this and and, as well as many other pages.
If possible, I think that I may have to somehow request a collection to get the sproc that I want to call, and then somehow reference the sproc, and not just call sumTotals() directly, as with a stand-alone language.
We are just starting to look at programming with DocumentDB, so we are not entirely sure what can be done with it. Thanks for any help or advice.