Cosmos DB. I want all my data in same collection. Can I achieve this with an unlimited collection?












0














At the momment I'm using Cosmos DB fixed 10 gb where I save all my data in same collection. I want to continue this way. So do I just need to create an unlimited collection. When I do that it ask for a partion key which I don't quite understand what it is for. I have tried to read about it but didn't get much clever. Hope you guys can help me.



Today I create documents like this. Should it be done in another way if using unlimited collection. Thinking about should i declare the partion key somewhere?:



protected async Task<bool> CreateDocumentAsync(Resource document)
{
var collectionUri = UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName, _db.Options.Value.CollectionName);

ResourceResponse<Document> result = null;

for (int i = 0; i < MaxRetryCount; i++)
{
try
{
result = await _db.Client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName, _db.Options.Value.CollectionName), document);
break;
}
catch (DocumentClientException dex) when (dex.StatusCode.HasValue && (int)dex.StatusCode.Value == 429)
{
_logger.LogWarning($"");
await Task.Delay(dex.RetryAfter);
}
}

if (result == null)
return false;

int statusCode = (int)result.StatusCode;
return statusCode >= 200 && statusCode < 300;
}









share|improve this question



























    0














    At the momment I'm using Cosmos DB fixed 10 gb where I save all my data in same collection. I want to continue this way. So do I just need to create an unlimited collection. When I do that it ask for a partion key which I don't quite understand what it is for. I have tried to read about it but didn't get much clever. Hope you guys can help me.



    Today I create documents like this. Should it be done in another way if using unlimited collection. Thinking about should i declare the partion key somewhere?:



    protected async Task<bool> CreateDocumentAsync(Resource document)
    {
    var collectionUri = UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName, _db.Options.Value.CollectionName);

    ResourceResponse<Document> result = null;

    for (int i = 0; i < MaxRetryCount; i++)
    {
    try
    {
    result = await _db.Client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName, _db.Options.Value.CollectionName), document);
    break;
    }
    catch (DocumentClientException dex) when (dex.StatusCode.HasValue && (int)dex.StatusCode.Value == 429)
    {
    _logger.LogWarning($"");
    await Task.Delay(dex.RetryAfter);
    }
    }

    if (result == null)
    return false;

    int statusCode = (int)result.StatusCode;
    return statusCode >= 200 && statusCode < 300;
    }









    share|improve this question

























      0












      0








      0







      At the momment I'm using Cosmos DB fixed 10 gb where I save all my data in same collection. I want to continue this way. So do I just need to create an unlimited collection. When I do that it ask for a partion key which I don't quite understand what it is for. I have tried to read about it but didn't get much clever. Hope you guys can help me.



      Today I create documents like this. Should it be done in another way if using unlimited collection. Thinking about should i declare the partion key somewhere?:



      protected async Task<bool> CreateDocumentAsync(Resource document)
      {
      var collectionUri = UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName, _db.Options.Value.CollectionName);

      ResourceResponse<Document> result = null;

      for (int i = 0; i < MaxRetryCount; i++)
      {
      try
      {
      result = await _db.Client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName, _db.Options.Value.CollectionName), document);
      break;
      }
      catch (DocumentClientException dex) when (dex.StatusCode.HasValue && (int)dex.StatusCode.Value == 429)
      {
      _logger.LogWarning($"");
      await Task.Delay(dex.RetryAfter);
      }
      }

      if (result == null)
      return false;

      int statusCode = (int)result.StatusCode;
      return statusCode >= 200 && statusCode < 300;
      }









      share|improve this question













      At the momment I'm using Cosmos DB fixed 10 gb where I save all my data in same collection. I want to continue this way. So do I just need to create an unlimited collection. When I do that it ask for a partion key which I don't quite understand what it is for. I have tried to read about it but didn't get much clever. Hope you guys can help me.



      Today I create documents like this. Should it be done in another way if using unlimited collection. Thinking about should i declare the partion key somewhere?:



      protected async Task<bool> CreateDocumentAsync(Resource document)
      {
      var collectionUri = UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName, _db.Options.Value.CollectionName);

      ResourceResponse<Document> result = null;

      for (int i = 0; i < MaxRetryCount; i++)
      {
      try
      {
      result = await _db.Client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName, _db.Options.Value.CollectionName), document);
      break;
      }
      catch (DocumentClientException dex) when (dex.StatusCode.HasValue && (int)dex.StatusCode.Value == 429)
      {
      _logger.LogWarning($"");
      await Task.Delay(dex.RetryAfter);
      }
      }

      if (result == null)
      return false;

      int statusCode = (int)result.StatusCode;
      return statusCode >= 200 && statusCode < 300;
      }






      c# nosql azure-cosmosdb






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 22:09









      Loc Dai Le

      4171237




      4171237
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The partition key value can be specified by providing the RequestOptions object with the PartitionKey value set.



          Your create document line would become:



          var requestOptions = new RequestOptions{
          PartitionKey = new PartitionKey("yourPartitionKeyValueHere");
          }

          result = await _db.Client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName, _db.Options.Value.CollectionName), document, requestOptions);


          However it does should like you don't understand CosmosDB partitioning which means you can get in really deep pitfall.



          The partition key definition should be a property that most of the times if not always, is known to you when you are doing querying or reading of documents.



          I highly recommend you watch this video. It will greatly help you understand partitioning: https://azure.microsoft.com/en-us/resources/videos/azure-documentdb-elastic-scale-partitioning/



          On a side note, I can see several mistakes in the way you are using the CosmosDB SDK which I cannot fix in a single answer. For example, the CosmosDB SDK has already logic to handle the retry policy logic on the document client level via the RetryOptions object so you don't need to write any custom code. I would suggest you read more of the CosmosDB documentation.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402347%2fcosmos-db-i-want-all-my-data-in-same-collection-can-i-achieve-this-with-an-unl%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The partition key value can be specified by providing the RequestOptions object with the PartitionKey value set.



            Your create document line would become:



            var requestOptions = new RequestOptions{
            PartitionKey = new PartitionKey("yourPartitionKeyValueHere");
            }

            result = await _db.Client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName, _db.Options.Value.CollectionName), document, requestOptions);


            However it does should like you don't understand CosmosDB partitioning which means you can get in really deep pitfall.



            The partition key definition should be a property that most of the times if not always, is known to you when you are doing querying or reading of documents.



            I highly recommend you watch this video. It will greatly help you understand partitioning: https://azure.microsoft.com/en-us/resources/videos/azure-documentdb-elastic-scale-partitioning/



            On a side note, I can see several mistakes in the way you are using the CosmosDB SDK which I cannot fix in a single answer. For example, the CosmosDB SDK has already logic to handle the retry policy logic on the document client level via the RetryOptions object so you don't need to write any custom code. I would suggest you read more of the CosmosDB documentation.






            share|improve this answer


























              0














              The partition key value can be specified by providing the RequestOptions object with the PartitionKey value set.



              Your create document line would become:



              var requestOptions = new RequestOptions{
              PartitionKey = new PartitionKey("yourPartitionKeyValueHere");
              }

              result = await _db.Client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName, _db.Options.Value.CollectionName), document, requestOptions);


              However it does should like you don't understand CosmosDB partitioning which means you can get in really deep pitfall.



              The partition key definition should be a property that most of the times if not always, is known to you when you are doing querying or reading of documents.



              I highly recommend you watch this video. It will greatly help you understand partitioning: https://azure.microsoft.com/en-us/resources/videos/azure-documentdb-elastic-scale-partitioning/



              On a side note, I can see several mistakes in the way you are using the CosmosDB SDK which I cannot fix in a single answer. For example, the CosmosDB SDK has already logic to handle the retry policy logic on the document client level via the RetryOptions object so you don't need to write any custom code. I would suggest you read more of the CosmosDB documentation.






              share|improve this answer
























                0












                0








                0






                The partition key value can be specified by providing the RequestOptions object with the PartitionKey value set.



                Your create document line would become:



                var requestOptions = new RequestOptions{
                PartitionKey = new PartitionKey("yourPartitionKeyValueHere");
                }

                result = await _db.Client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName, _db.Options.Value.CollectionName), document, requestOptions);


                However it does should like you don't understand CosmosDB partitioning which means you can get in really deep pitfall.



                The partition key definition should be a property that most of the times if not always, is known to you when you are doing querying or reading of documents.



                I highly recommend you watch this video. It will greatly help you understand partitioning: https://azure.microsoft.com/en-us/resources/videos/azure-documentdb-elastic-scale-partitioning/



                On a side note, I can see several mistakes in the way you are using the CosmosDB SDK which I cannot fix in a single answer. For example, the CosmosDB SDK has already logic to handle the retry policy logic on the document client level via the RetryOptions object so you don't need to write any custom code. I would suggest you read more of the CosmosDB documentation.






                share|improve this answer












                The partition key value can be specified by providing the RequestOptions object with the PartitionKey value set.



                Your create document line would become:



                var requestOptions = new RequestOptions{
                PartitionKey = new PartitionKey("yourPartitionKeyValueHere");
                }

                result = await _db.Client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName, _db.Options.Value.CollectionName), document, requestOptions);


                However it does should like you don't understand CosmosDB partitioning which means you can get in really deep pitfall.



                The partition key definition should be a property that most of the times if not always, is known to you when you are doing querying or reading of documents.



                I highly recommend you watch this video. It will greatly help you understand partitioning: https://azure.microsoft.com/en-us/resources/videos/azure-documentdb-elastic-scale-partitioning/



                On a side note, I can see several mistakes in the way you are using the CosmosDB SDK which I cannot fix in a single answer. For example, the CosmosDB SDK has already logic to handle the retry policy logic on the document client level via the RetryOptions object so you don't need to write any custom code. I would suggest you read more of the CosmosDB documentation.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 at 23:44









                Nick Chapsas

                2,5011314




                2,5011314






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402347%2fcosmos-db-i-want-all-my-data-in-same-collection-can-i-achieve-this-with-an-unl%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen