Reputation: 746
I cannot read single document by id from DocumentDB. I am building application and I have exact problem like this one. I don't understand what am I missing. Let's say I have Users collection and I have /UserId for my partition key, I cannot read document I always receive "Resource Not Found". It is confusing because according to Microsoft's documentation I can only supply Uri to ReadDocumentAsync
method, but doing that I get error partition key is required. I inderstand partition keys, but I have hard time understanding how are they connected to document. For example, my partition key is /UserId, which is a path, for method ReadDocumentAsync
I actually have to provide value of partition key. And I have hard time understanding what is that value.
I attempted creating document with c# code and retrieving same document with ID created by system (GUID) it does not work... This sound simple and basic, but can someone point out to me where am I wrong? Or show me a reference to C# .NET Core code that I can use, sample collection, sample document and sample code. Microsoft's documentation/sample code does not work for me.
My code looks like this:
documentId = "1c230b2d-1e37-9e02-d3a0-10364a188487";
collectionId = "Users";
RequestOptions options = new RequestOptions();
options.PartitionKey = new PartitionKey("201801-M");
var document = await _serverConnection.ReadDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, documentId),options);
return document;
My DB looks like this:
And my Partition Key looks like this:
Upvotes: 1
Views: 1110
Reputation: 337
I had similar problem when I received "Entity with the specified id does not exist in the system" while I could see it in Data Explorer on Azure Portal. Turns out, I used integer as Partition key in json and had to set it the same way in request:
var documentId1 = "150:060e7249-11cd-4d5c-b666-7711d9498ab4";
var collectionId = _collectionName;
var databaseId = _databaseName;
RequestOptions options1 = new RequestOptions();
options1.PartitionKey = new PartitionKey(150);
var document = await _documentClient.ReadDocumentAsync(
UriFactory.CreateDocumentUri(databaseId, collectionId, documentId1),options1);
So if I write:
options1.PartitionKey = new PartitionKey("150");
request won't work.
Upvotes: 0
Reputation: 311
I had this problem. The solution for me was ensuring that the PartitionKey with its value were written to the json data object exactly as it is in the collection. For example, if your collection's partition key is /state and you want a value of "Florida" then in your json data object must contain the following in your json data: "state":"Florida" in your WriteDocumentAsync. In my case I am using dynamic json data so I had to inject the partitionkey and value into my json data object. If it is not done correctly you will get and error, Entity with specified id was not found when you try to read the data. You have two choice: 1 write the data out with a matching partition key to the collection's or 2 in your request options use "Undefined.Value" for the partition key. Using the Undefined.Value will find any matching id in the collection. When reading the document back using ReadDocumentAsync you must set your RequestOptions.PartitionKey to your desired partition key value you are seeking. Hope this helps.
Upvotes: 0
Reputation: 746
It is my emulator issue that I use on a machine. I have connected to actual CosmosDB and it works fine... My emulator was buggy and throws errors, stops working, starts working and as soon as I connected to Azure it was fine retrieving sample data.
Upvotes: 1