在DocumentDB中插入json文档

时间:2015-08-26 12:19:58

标签: c# azure-cosmosdb

在DocumentDB文档示例中,我找到了C#对象的插入。

Trait

在我的情况下,我从应用程序客户端接收json字符串,并希望将它们插入DocumentDB而不反序列化它们。无法找到做类似事情的任何例子。

真心感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:6)

published .NET Sample code -

复制
    private static async Task UseStreams(string colSelfLink)
    {
        var dir = new DirectoryInfo(@".\Data");
        var files = dir.EnumerateFiles("*.json");
        foreach (var file in files)
        {
            using (var fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
            {
                Document doc = await client.CreateDocumentAsync(colSelfLink, Resource.LoadFrom<Document>(fileStream));
                Console.WriteLine("Created Document: ", doc);
            }
        }

        //Read one the documents created above directly in to a Json string
        Document readDoc = client.CreateDocumentQuery(colSelfLink).Where(d => d.Id == "JSON1").AsEnumerable().First();
        string content = JsonConvert.SerializeObject(readDoc);

        //Update a document with some Json text, 
        //Here we're replacing a previously created document with some new text and even introudcing a new Property, Status=Cancelled
        using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes("{\"id\": \"JSON1\",\"PurchaseOrderNumber\": \"PO18009186470\",\"Status\": \"Cancelled\"}")))
        {
            await client.ReplaceDocumentAsync(readDoc.SelfLink, Resource.LoadFrom<Document>(memoryStream));
        }
    }