Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. DocumentClient _documentDbclient;
  2.  
  3. // Database configuration
  4. static readonly string DocumentDbKeyConfig = "3dzS7T3a8lgEblkEnzSsdfasdfasf2omI1cp7==";
  5. static readonly string DocumentDbEndPointConfig = "https://your-docdb.documents.azure.com:443/";
  6. static readonly string DocumentDbDatabaseNameConfig = "your-database-name";
  7.  
  8.  
  9. void Main()
  10. {
  11.  
  12. string collectionName = "TestCollection";
  13.  
  14. Uri uri = UriFactory.CreateDocumentCollectionUri(DocumentDbDatabaseNameConfig, collectionName);
  15. InitializeDocumentDbAsync(DocumentDbDatabaseNameConfig).ConfigureAwait(false);
  16.  
  17. createDocumentCollectionIfNotExistAsync(DocumentDbDatabaseNameConfig, collectionName).ConfigureAwait(false);
  18.  
  19. DocumentCollection collection = _documentDbclient.ReadDocumentCollectionAsync(uri).Result;
  20. var query = _documentDbclient.CreateDocumentQuery(uri);
  21. var queryList = query.ToList();
  22.  
  23. int index = 0;
  24. int count = 50; // number of item to fetch at a time
  25. while (true)
  26. {
  27. var result = queryList.Skip(index * count).Take(count);
  28.  
  29. if (!result.Any())
  30. {
  31. // end iterating.
  32. return;
  33. }
  34.  
  35. foreach (Document element in result)
  36. {
  37. JObject j = (dynamic)element;
  38. j["Type "] = 8; // edit value.
  39.  
  40. var upsertedResult = _documentDbclient.UpsertDocumentAsync(uri, j, null, true).Result;
  41. }
  42. ++index;
  43. }
  44. }
  45.  
  46. public async Task InitializeDocumentDbAsync(string databaseName)
  47. {
  48. // Create a new instance of the DocumentClient
  49. _documentDbclient = new DocumentClient(
  50. new Uri(DocumentDbEndPointConfig), DocumentDbKeyConfig);
  51. // Check if the database FamilyDB does not exist
  52.  
  53. try
  54. {
  55. await _documentDbclient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseName));
  56. }
  57. catch (DocumentClientException de)
  58. {
  59. // If the database does not exist, create a new database
  60. if (de.StatusCode == HttpStatusCode.NotFound)
  61. {
  62. await _documentDbclient.CreateDatabaseAsync(new Database { Id = databaseName });
  63. }
  64. else
  65. {
  66. throw;
  67. }
  68. }
  69. catch (Exception e)
  70. {
  71. throw;
  72. }
  73. }
  74.  
  75. private async Task createDocumentCollectionIfNotExistAsync(string databaseName, string collectionName)
  76. {
  77. try
  78. {
  79. await
  80. _documentDbclient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName));
  81. }
  82. catch (DocumentClientException de)
  83. {
  84. // not exist
  85. throw;
  86. }
  87. catch (Exception e)
  88. {
  89. // do nothing.
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement