Introduction

Let’s start.
    1. Create a new console app.
    2. Right-click on Project -> Manage NuGet Package.
    3. Search for ‘documentdb’ under the "Browse" tab.
    4. Select Microsoft.Azure.DocumentDB package and click on Install.
      Cosmos DB
You can see that CreateDocumentCollectionIfNotExistsAsync accepts two parameters.
  1. Database Link (path to database): dbo/testDb
  2. Document Collection Object
    Alternatively, you can use UriFactory.CreateDatabaseUri(databaseName) method to create database URI for you.
    1. //Create new database collection
    2. Console.WriteLine("\r\n>>>>>>>>>>>>>>>> Creating Collection <<<<<<<<<<<<<<<<<<<");
    3. var collectionDefinition = new DocumentCollection { Id = "testDocumentCollection" };
    4. var collection = await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("testDb"),
    5. collectionDefinition);
    6. Console.WriteLine("Collection testDocumentCollection created successfully");
Here, doc1Definatin defines the document we want to insert in Cosmos DB.
UriFactory.CreateDocumentCollectionUri("testDb", "testDocumentCollection") method is used to create the collection URI “dbo/testDb/colls/testDocumentCollection”.
Query Inserted Document
  1. Console.WriteLine("\r\n>>>>>>>>>>>> Querying Document <<<<<<<<<<<<<<<<<<<<");
  2. var response = client.CreateDocumentQuery
  3. (UriFactory.CreateDocumentCollectionUri("testDb", "testDocumentCollection"),
  4. "select * from c").ToList();
  5. var document = response.First();
  6. Console.WriteLine($"Id:{document.id}");
  7. Console.WriteLine($"Title:{document.title}");
  8. Console.WriteLine($"Rank:{document.rank}");
  9. Console.WriteLine($"category:{document.category}");
CreateDocumentQuery method requires two parametes,
  1. Collection URI
  2. Query string
Delete Collection
  1. Console.WriteLine("\r\n>>>>>>>>>>>>>>>> Deleteing Collection <<<<<<<<<<<<<<<<<<<");
  2. await client.DeleteDocumentCollectionAsync(
  3. UriFactory.CreateDocumentCollectionUri("testDb", "testDocumentCollection"));
Overall Code
  1. using Microsoft.Azure.Documents;
  2. using Microsoft.Azure.Documents.Client;
  3. using System;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace CosmosDBCrudOperationd
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Task.Run(async () =>
  14. {
  15. var endpoint = ConfigurationManager.AppSettings["DocDbEndpoint"];
  16. var masterKey = ConfigurationManager.AppSettings["DocDbMasterKey"];
  17. using (var client = new DocumentClient(new Uri(endpoint), masterKey))
  18. {
  19. Console.WriteLine("\r\n>>>>>>>>>>>>>>>> Creating Database <<<<<<<<<<<<<<<<<<<");
  20. // Create new database Object
  21. //Id defines name of the database
  22. var databaseDefinition = new Database { Id = "testDb" };
  23. var database = await client.CreateDatabaseIfNotExistsAsync(databaseDefinition);
  24. Console.WriteLine("Database testDb created successfully");
  25. //Create new database collection
  26. Console.WriteLine("\r\n>>>>>>>>>>>>>>>> Creating Collection <<<<<<<<<<<<<<<<<<<");
  27. var collectionDefinition = new DocumentCollection { Id = "testDocumentCollection" };
  28. var collection = await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("testDb"),
  29. collectionDefinition);
  30. Console.WriteLine("Collection testDocumentCollection created successfully");
  31. //Insert new Document
  32. Console.WriteLine("\r\n>>>>>>>>>>>>>>>> Creating Document <<<<<<<<<<<<<<<<<<<");
  33. dynamic doc1Definition = new
  34. {
  35. title = "Star War IV ",
  36. rank = 600,
  37. category = "Sci-fi"
  38. };
  39. var document1 = await client.CreateDocumentAsync(
  40. UriFactory.CreateDocumentCollectionUri("testDb", "testDocumentCollection"),
  41. doc1Definition);
  42. Console.WriteLine("\r\n>>>>>>>>>>>> Querying Document <<<<<<<<<<<<<<<<<<<<");
  43. var response = client.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri("testDb", "testDocumentCollection"),
  44. "select * from c").ToList();
  45. var document = response.First();
  46. Console.WriteLine($"Id:{document.id}");
  47. Console.WriteLine($"Title:{document.title}");
  48. Console.WriteLine($"Rank:{document.rank}");
  49. Console.WriteLine($"category:{document.category}");
  50. Console.WriteLine("\r\n>>>>>>>>>>>>>>>> Deleteing Collection <<<<<<<<<<<<<<<<<<<");
  51. await client.DeleteDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri("testDb", "testDocumentCollection"));
  52. Console.ReadKey();
  53. }
  54. }).Wait();
  55. }
  56. }
  57. }