【问题标题】:Inserting json documents in DocumentDB在 DocumentDB 中插入 json 文档
【发布时间】:2015-08-27 03:28:38
【问题描述】:

在 DocumentDB 文档示例中,我发现插入了 C# 对象。

// Create the Andersen family document.
Family AndersenFamily = new Family
{
    Id = "AndersenFamily",
    LastName = "Andersen",
    Parents =  new Parent[] {
        new Parent { FirstName = "Thomas" },
        new Parent { FirstName = "Mary Kay"}
    },
    IsRegistered = true
};

await client.CreateDocumentAsync(documentCollection.DocumentsLink, AndersenFamily);

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

真诚感谢任何帮助..

谢谢

【问题讨论】:

    标签: c# azure-cosmosdb


    【解决方案1】:

    复制自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));
            }
        }
    

    【讨论】:

    • 什么是资源?
    • @JonathanFishbein 这是 DocumentDB .Net SDK 中的一个类。
    猜你喜欢
    • 2018-05-15
    • 1970-01-01
    • 2021-09-17
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多