如何使用c#在documetDB中存储和检索和删除列表

时间:2015-06-04 09:07:27

标签: azure azure-cosmosdb

我需要知道如何在Document DB中存储和检索列表。 我有一个列表然后我需要将列表序列化为json对象并存储到Document DB中。

2 个答案:

答案 0 :(得分:0)

我建议开始here。 DocumentDb可以很容易地保存任何对象,甚至可以自动为您执行JSON序列化。

答案 1 :(得分:0)

Sudheer - 您是否尝试将JSON作为字符串并将其存储在DocumentDB中或存储对象? .Net示例显示了如何执行后者,但简而言之,您可以创建POCO对象列表,然后迭代它们,将它们写入数据库。或者,如果您希望将单独的JSON文档写为单个事务,则可以编写存储过程。

以下是POCO列表的示例。请注意,在SalesOrder定义的定义中,该属性具有将其映射到数据库ID的属性:

[JsonProperty(PropertyName =“id”)] 公共字符串Id {get;组; }

var orders = new List();

        orders.Add(new SalesOrder 
        { 
            Id = "POCO1", 
            PurchaseOrderNumber = "PO18009186470", 
            OrderDate = new DateTime(2005, 7, 1), 
            AccountNumber = "10-4020-000510", 
            SubTotal = 419.4589m, 
            TaxAmt = 12.5838m, 
            Freight = 472.3108m, 
            TotalDue = 985.018m, 
            Items = new[] 
            { 
                new SalesOrderDetail 
                { 
                    OrderQty = 1, 
                    ProductId = 760, 
                    UnitPrice = 419.4589m, 
                    LineTotal = 419.4589m 
                } 
            }, 
        }); 

        orders.Add(new SalesOrder2 
        { 
            Id = "POCO2", 
            PurchaseOrderNumber = "PO15428132599", 
            OrderDate = new DateTime(2005, 7, 1), 
            DueDate = new DateTime(2005, 7, 13), 
            ShippedDate = new DateTime(2005, 7, 8), 
            AccountNumber = "10-4020-000646", 
            SubTotal = 6107.0820m, 
            TaxAmt = 586.1203m, 
            Freight = 183.1626m, 
            DiscountAmt = 1982.872m,            
            TotalDue = 4893.3929m, 
            Items = new[] 
            { 
                new SalesOrderDetail2 
                { 
                    OrderQty = 3, 
                    ProductCode = "A-123",       
                    ProductName = "Product 1",  
                    CurrencySymbol = "$",       
                    CurrencyCode = "USD",       
                    UnitPrice = 17.1m,            
                    LineTotal = 5.7m  
                }, 
            } 
        }); 

        foreach (var order in orders) 
        { 
            Document created = await client.CreateDocumentAsync(colSelfLink, order); 
            Console.WriteLine("Created SalesOrder: " + created); 
        }