question

JamesGFoster-9024 avatar image
0 Votes"
JamesGFoster-9024 asked JamesGFoster-9024 commented

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

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
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It turns out that our actual issue dealt with an issue in how "Content-Type" was handled. When we modified the encoding of the body, things worked fine. The suggestion to try it out with Postman was helpful in getting us in the right direction.

0 Votes 0 ·

1 Answer

AnuragSharma-MSFT avatar image
0 Votes"
AnuragSharma-MSFT answered JamesGFoster-9024 commented

Hi @JamesGFoster-9024, 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'



image.png (63.9 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi AnuragSharma,

Rather than a GET request for all documents (which works fine for me), could you try a POST with a SQL query as described https://docs.microsoft.com/en-us/azure/cosmos-db/partitioning-overview?

0 Votes 0 ·

Sorry, wrong link. The query discussion is at https://docs.microsoft.com/en-us/azure/cosmos-db/tutorial-query-sql-api.

0 Votes 0 ·

Just to clarify, I'm not trying to get "a list of documents" but selected fields from the documents. That is why I structured this as a POST with a query.

I'm trying to set up Postman but having trouble with a couple things. First, how do you define the variable RFC1123time? Next, how do you define the variable authToken? Our application goes through the RBAC authentication process, but that seems like it would be difficult to do manually in Postman. I have a Primary Key for the database, but it looks to me like the process of generating an authorization token is non-trivial.

0 Votes 0 ·

Thanks for your reply. We can do POST as well with selected fields. We can select the headers as mentioned in the below screenshot:

122675-image.png

Then the body to select desired fields:

122599-image.png

Finally the response has just mentioned fields:

122627-image.png

To set up postman, you can download the zip from below link and just import on the Postman.

https://github.com/liupeirong/cosmosdb-postman-collection

Then you just need to set the environment variables with host url and master key(primary key). All other variable are set RFC1123time and authToken are set in the pre-request script which is by default imported with the zip, we dont need to make any changes.

122721-image.png

Please let me know if this helps.




1 Vote 1 ·
image.png (65.9 KiB)
image.png (33.9 KiB)
image.png (24.4 KiB)
image.png (49.1 KiB)
Show more comments