- DocumentDB - Visualize Data
- DocumentDB - Access Control
- DocumentDB - Data Migration
- DocumentDB - Partitioning
- DocumentDB - Geospatial Data
- DocumentDB - Indexing Records
- DocumentDB - Sorting Records
- DocumentDB - Limiting Records
- DocumentDB - Data Types
- DocumentDB - Data Modeling
- DocumentDB - Delete Document
- DocumentDB - Update Document
- DocumentDB - Query Document
- DocumentDB - Insert Document
- DocumentDB - Delete Collection
- DocumentDB - Create Collection
- DocumentDB - Drop Databases
- DocumentDB - List Databases
- DocumentDB - Create Database
- DocumentDB - Connect Account
- DocumentDB - Create Account
- DocumentDB - Environment Setup
- DocumentDB - Advantages
- DocumentDB - Introduction
- DocumentDB - Home
DocumentDB Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
DocumentDB - Update Document
In this chapter, we will learn how to update the documents. Using Azure portal, you can easily update document by opening the document in Document explorer and updating it in editor pke a text file.
Cpck ‘Save’ button. Now when you need to change a document using .Net SDK you can just replace it. You don t need to delete and recreate it, which besides being tedious, would also change the resource id, which you wouldn t want to do when you re just modifying a document. Here are the following steps to update the document using .Net SDK.
Let’s take a look at the following ReplaceDocuments task where we will query for documents where the isNew property is true, but we will get none because there aren t any. So, let s modify the documents we added earper, those whose names start with New Customer.
Step 1 − Add the isNew property to these documents and set its value to true.
private async static Task ReplaceDocuments(DocumentCpent cpent) { Console.WriteLine(); Console.WriteLine(">>> Replace Documents <<<"); Console.WriteLine(); Console.WriteLine("Quering for documents with isNew flag"); var sql = "SELECT * FROM c WHERE c.isNew = true"; var documents = cpent.CreateDocumentQuery(collection.SelfLink, sql).ToList(); Console.WriteLine("Documents with isNew flag: {0} ", documents.Count); Console.WriteLine(); Console.WriteLine("Quering for documents to be updated"); sql = "SELECT * FROM c WHERE STARTSWITH(c.name, New Customer ) = true"; documents = cpent.CreateDocumentQuery(collection.SelfLink, sql).ToList(); Console.WriteLine("Found {0} documents to be updated", documents.Count); foreach (var document in documents) { document.isNew = true; var result = await cpent.ReplaceDocumentAsync(document._self, document); var updatedDocument = result.Resource; Console.WriteLine("Updated document isNew flag: {0}", updatedDocument.isNew); } Console.WriteLine(); Console.WriteLine("Quering for documents with isNew flag"); sql = "SELECT * FROM c WHERE c.isNew = true"; documents = cpent.CreateDocumentQuery(collection.SelfLink, sql).ToList(); Console.WriteLine("Documents with isNew flag: {0}: ", documents.Count); Console.WriteLine(); }
Step 2 − Get the documents to be updated using the same STARTSWITH query and that gives us the documents, which we are getting back here as dynamic objects.
Step 3 − Attach the isNew property and set it to true for each document.
Step 4 − Call ReplaceDocumentAsync, passing in the document s SelfLink, along with the updated document.
Now just to prove that this worked, query for documents where isNew equaled true. Let’s call the above queries from the CreateDocumentCpent task.
private static async Task CreateDocumentCpent() { // Create a new instance of the DocumentCpent using (var cpent = new DocumentCpent(new Uri(EndpointUrl), AuthorizationKey)) { database = cpent.CreateDatabaseQuery("SELECT * FROM c WHERE c.id = myfirstdb ").AsEnumerable().First(); collection = cpent.CreateDocumentCollectionQuery(database.CollectionsLink, "SELECT * FROM c WHERE c.id = MyCollection ").AsEnumerable().First(); //await CreateDocuments(cpent); //QueryDocumentsWithSql(cpent); //await QueryDocumentsWithPaging(cpent); //QueryDocumentsWithLinq(cpent); await ReplaceDocuments(cpent); } }
When the above code is compiled and executed, you will receive the following output.
**** Replace Documents **** Quering for documents with isNew flag Documents with isNew flag: 0 Quering for documents to be updated Found 2 documents to be updated Updated document ‘isNew’ flag: True Updated document ‘isNew’ flag: True Quering for documents with isNew flag Documents with isNew flag: 2Advertisements