Why does a Cosmos DB REST API query require a partition key?

James G Foster 46 Reputation points
2021-08-11T16:41:26.1+00:00

I've created a database and a collection. When creating the collection, the partition key was a required field and the hint suggested that '/id' would be a good choice so I used that.

Now I'm trying to query the database following the instructions here. The sample code provided does not include a partition key but when I do a query following that example, I get the following error: The partition key supplied in x-ms-partitionkey header has fewer components than defined in the the collection.

Note first that this error message is itself wrong. the missing header is x-ms-documentdb-partitionkey.

Next, when I add a header with a partition key, I get a new error: The input content is invalid because the required properties - 'id; ' - are missing.

If I add an 'id' to the body then I get a new error: Entity with the specified id already exists in the system.

Here is a portion of my Dart code:

   var url ='https://${client.account}.documents.azure.com/dbs/$databaseName/colls/$_projects/docs';  
       var uri = Uri.parse(url);  
       var headers = {  
         'Access-Control-Allow-Origin': '*',  
         'Access-Control-Allow-Headers': '*',  
         'Accept': 'application/json',  
         'Content-Type': 'application/query+json',  
         'Authorization': 'type=aad&ver=1.0&sig=${token['access_token']}',  
         'x-ms-date': DateTime.now().asRfc1123(),  
         'x-ms-version': '2018-12-31',  
         'x-ms-documentdb-isquery': 'True',  
         'x-ms-documentdb-query-enablecrosspartition': 'True',  
       };  
       var body = {  
         'query':  
             'SELECT p.id, p.name FROM Projects p',  
             'parameters': [],  
       };  
       var response;  
       try {  
         response = await http.post(uri, headers: headers, body: jsonEncode(body));  
       } catch (e) {  
         print(e.toString());  
       }  
       print(response.statusCode);  
       print(jsonDecode(response.body));  

Why does a cross-partition query require a partition key? What value should I give for the partition key if the partition key is one of the fields I don't know and want returned?

I want a list of names from the database. Is that too much to ask?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,659 questions
{count} votes

Accepted answer
  1. Anurag Sharma 17,606 Reputation points
    2021-08-11T18:51:12.07+00:00

    Hi @James G Foster , welcome to Microsoft Q&A forum and apologies for the trouble you are facing.

    Thanks for the detailed description of the issue. What I understand from the details provided is we want to get list of documents from a collection in Azure Cosmos DB through REST apis.

    I was trying to do to a get request for all the documents in the collection using Postman, I did not pass the x-ms-documentdb-query-enablecrosspartition in header and was able to retrieve the response successfully across the partitions. Please check the list of headers I am passing:

    122369-image.png

    Even in the below articles, x-ms-documentdb-query-enablecrosspartition is mentioned as optional field:

    Querying Azure Cosmos DB resources using the REST API
    List (ReadFeed) Documents

    Please try it with the headers as mentioned in the screenshot and let us know if this works. We can discuss further based on your response.

    ----------

    if answer helps, you can mark it 'Accept Answer'


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.