Cosmos DB. I want all my data in same collection. Can I achieve this with an unlimited collection?
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
add a comment |
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
add a comment |
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
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
c# nosql azure-cosmosdb
asked Nov 20 '18 at 22:09
Loc Dai LeLoc Dai Le
4281237
4281237
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 20 '18 at 23:44
Nick ChapsasNick Chapsas
2,9961515
2,9961515
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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