0
votes

We all know that we can save one document at a time in cosmos DB using Azure functions (using out parameter or using return) like:

 object outputDocument = new { id = Guid.NewGuid().ToString(), 
                                   customObjList= objList 
                             };

where objList is a list of custom object. Now the above outputDocument will return one json Document and the data in DB will be saved in the below format.

{
    "id" : "....",
    "customObjList" :[ 
        {
            "_id" : "81afbe1a-3da0-4143-9dc6-0b3bf5252e0d",
            ...
        }, 
        {
            "_id" : "2af7e1ac-15ca-424e-8af1-d3e5a2cca8de",
            ....
        },
        .....
]}

But what I want is something like below list of documents to be saved directly from Azure functions

 /* 1 */
    {
     "_id" : "81afbe1a-3da0-4143-9dc6-0b3bf5252e0d",
     ...
    }
    /* 2 */
    {
      "_id" : "2af7e1ac-15ca-424e-8af1-d3e5a2cca8de",
       ....
    }

[To me the requirement is to fetch the data from rss feed and save it to cosmos DB, which I am first reading it and storing the data in a list of object but unable to save those list of object independently in cosmos DB]

1
Why not just do a direct write (or writes) to Cosmos DB, from your function?David Makogon

1 Answers

1
votes

You can save multiple documents at a time by using ICollector or if you have an async function, using the IAsyncCollector.

This is how you would output multiple objects:

public static void Run(ICollector<object> myQueueItem, TraceWriter log)
{
    foreach (object obj in objList)
    {
        var objnew = { 
                        id = Guid.NewGuid().ToString(), 
                        someProperty = obj.someProperty
                     };

        myQueueItem.Add(objnew);
    }
}

Source: Azure Documentation -- Writing multiple output values