簡體   English   中英

將對象列表從Azure函數保存到cosmos DB(文檔DB或mongo DB)

[英]save the list of object to cosmos DB (Document DB or mongo DB) from Azure functions

我們都知道,我們可以使用Azure函數(使用out參數或使用return)一次將一個文檔保存在cosmos DB中:

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

其中objList是自定義對象的列表。 現在,上面的outputDocument將返回一個json文檔,並且DB中的數據將以以下格式保存。

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

但是我想要的是下面直接從Azure函數保存的文檔列表下面的內容

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

[對我來說,要求是從rss feed中獲取數據並將其保存到cosmos DB,我首先要讀取它並將數據存儲在對象列表中,但無法將這些對象列表獨立保存在cosmos DB中]

您可以使用ICollector一次保存多個文檔,或者如果您具有異步功能,可以使用IAsyncCollector一次保存多個文檔。

這是您輸出多個對象的方式:

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);
    }
}

資料來源: Azure文檔-編寫多個輸出值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM