Search code examples
c#.net-coreazure-cosmosdbdocument-database

Reading single document from DocumentDB


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:

enter image description here

And my Partition Key looks like this:

enter image description here


Solution

  • 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.