Tuesday 6 June 2017

How to use Loop to execute store procedure in Cosmos-DB/Document-DB?

I have JSON like

{
  "id": "58d99ca3231f13b9ecbbbca4",
  "50records": [
    {
      "aomsLineNbr": 1,
      "licenses": [
        {
          "productKey": "84fc2cde-9735-4cea-b97a-3cd627d3d0a5",
          "aid": "someAid"   
        }
      ]
    }
  ]
}

  • I want to fetch record on the basis of aid.

  • 50record can have multiple objects and licenses can also have multiple objects.

  • I am constucting the query as "SELECT * FROM orders o WHERE o['50records'][0].licenses[0].aid='someAid'"
  • how can I loop those 50records and licenses to search aid in all available objects?

Below is my store procedure:

       function getOrdersByAidCollection(aid){

        var context = getContext();
        var collection = context.getCollection();
        var link = collection.getSelfLink();
        var response = context.getResponse();

        var query = "SELECT * FROM orders o WHERE o['50records'][0].licenses[0].aid='"+aid+"'";
        var isAccepted = collection.queryDocuments(
            collection.getSelfLink(),
            query,
            function (err, feed, options) {
                if (err) {
                    return errorResponse(400, err.message);
                }
                if (!feed || !feed.length){
                    return errorResponse(400, "no orders doc found");
                }else { 
                   getContext().getResponse().setBody(JSON.stringify(feed));
                }
            });

        if (!isAccepted){
            return errorResponse(400, "The query was not accepted by the server.");
        }


    }

Where and how I need to put a loop ??

Any help will be appreciable!

Thanks



via Anand Deshmukh

No comments:

Post a Comment