Reputation: 14145
I just started to scratch the surface with cosmosdb and I'm having issues with querying document. My document looks like this:
{
"id": "2",
"url": "cars/toyota-auris",
"name": "Toyota Auris",
"description": "Auris desc",
"address": {
"city": "Berlin"
},
"_rid": "--4zAKfcoCgCAAAAAAAAAA==",
"_self": "dbs/--2zAA==/colls/--4zAKfcoCg=/docs/--4zAKfcoCgCAAAAAAAAAA==/",
"_etag": "\"23010db4-0000-0d00-0000-5f1b5f4a0000\"",
"_attachments": "attachments/",
"_ts": 1595629383
}
Partition key: address/city
I'm trying to retrieve the document using cosmosd .net sdk 3.
int id = "2";
ItemResponse<Car> response = await _container.ReadItemAsync<Car>(id, new PartitionKey(id));
return response.Resource;
This query ends up with exception:
Microsoft.Azure.Cosmos.CosmosException: 'Response status code does not indicate success: NotFound (404); Substatus: 0; ActivityId: c2c2bbff-5451-44af-a397-ca67088cce02; Reason: ({
"Errors": [
"Resource Not Found"
]
});'
What's interesting is that I'm able is query all documents using
_container.GetItemQueryIterator<Car>(new QueryDefinition("SELECT * FROM Cars"));
What I'm doing wrong with querying single document?
Upvotes: 0
Views: 2758
Reputation: 8763
As indicated above you need to pass in the correct partition key value. Rewrite ReadItemAsync() similar to this.
int id = "2";
string pk = "Berlin";
ItemResponse<Car> response = await _container.ReadItemAsync<Car>(id, new PartitionKey(pk));
return response.Resource;
Upvotes: 1
Reputation: 7553
It looks like you are querying using id
as partition key, but your defined partition key is address/city
. Since ReadItemAsync only looks in the specified partition, it won't find your item. If you want to do point reads, you need to choose a partition key you know in advance.
Upvotes: 1