6

In DocumentDB documentation examples, I find insertion of C# objects.

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

In my case, I'm receiving json strings from application client and would like to insert them in DocumentDB without deserializing them. Could not find any examples of doing something similar.

Any help is sincerely appreciated..

Thanks

0

1 Answer 1

8

Copied from the 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));
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

What is Resource?
@JonathanFishbein It's a class in the DocumentDB .Net SDK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.