Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
Recursive CosmosDB stored procedure to return nested data without response.setBody()

I'm in the process of building a stored procedure in CosmosDB. The purpose of the stored procedure is to recursively get all child documents which relates to a parent document in my collection.

The steps are as follows:

  • Get parent documents
  • For each parent documents call MyRecursiveFunction
  • MyRecursiveFunction gets child documents for the parent document
  • MyRecursiveFunction should return child documents to the parent

.

function GetGroups(Id) {
    var context = getContext();
    var collection = context.getCollection();
    var response = context.getResponse();

    query = "MY QUERY"

    var accepted = collection.queryDocuments(collection.getSelfLink(), query,
        function (err, documents, responseOptions) {
            if (err) {
                throw new Error("Error" + err.message);
            }
            if (documents.length === 0) {
                throw "Unable to find document"
            }
            for (let i = 0; i < documents.length; i++) {
                MyRecursiveFunction(documents[i]) //<--- Assemble hierarchy
            }
        }
    )
    if (!accepted) {
        throw "Some error happened"
    }

    function MyRecursiveFunction(document) {
        for (let i = 0; i < document.children.length; i++) {

            query = "MY QUERY TO GET DOCUMENT BY ID"

            var accepted = collection.queryDocuments(collection.getSelfLink(), query,
                function (err, child, responseOptions) {
                    if (err) {
                        throw new Error("Error" + err.message);
                    }
                    if (child.length === 0) {
                        throw "Unable to find documents"
                    }
                    return child[0] //<------ How do I return this to parent caller?
                }
            )
            if (!accepted) {
                throw "Some error happened"
            }
        }
    }
}

The problem here is, that I'm having a hard time understanding how I can return the data which were fetched inside MyRecursiveFunction. The only way in which I seem to be able to do so, is if I do response.setBody(child[0]) - But obviously this sends a response to my API.

Using the returned accept property from collection.queryDocuments() only returns true or false

Therefore, my question boil down to the following:

  • How do I return the data from MyRecursiveFunction to the calling parent function?
  • In case I want to use a continuation token and I achieve to return data to my parent function, will this way of structuring my Stored procedure limit my possibility to use a continuation token, since its basically two individual calls?
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!