1
votes

I am try to query a DocumentDB that has a partition key using Node.js but it fails. According to here - https://stackoverflow.com/a/43002819/7799859 - I need to pass the partition key is some object that is called options.

Can anyone tell me where I can find examples for this options.partitionKey or options.enableCrossPartitionQuery?

What is this options object? How do I create it in NodeJS?

This is my code:

find: function (querySpec, callback)
{
    var self = this;
    console.log(self.collection);
    console.log(self.collection._self);
    console.log(this.collectionUrl);
    var options = {
            partitionKey: "myCustomKey"
    };
    self.client.queryDocuments(this.collectionUrl, querySpec,options).toArray(function (err, results) {
        if (err) {
            callback(err);
        } else {
            callback(null, results);
        }
    });
}

I added the options var, but when I send it no results are returned.

When I don't send it, this is the error:

{"code":"BadRequest","message":"Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception.\r\nActivityId: e4103506-d352-46d5-97d0-740d89065955"}

I am using Node version 4.2.6 and the latest version of documentdb. @GaryLiu-MSFT this is from package.json file:- "documentdb": "^1.10.0",

This is my querySpec:

var querySpec = { query: 'SELECT * FROM root', };

2
Difficult to give you any help, since you haven't shared any code. You simply state "but it fails." What fails? What is the error message? What is your query? What does your data look like? Please edit your question, as right now it's unclear what you're asking. - David Makogon
Also - the part about looking for examples... is off-topic (tool/tutorial recommendations are expressly off-topic). There are lots of examples floating around on the web, including in the sdk github repo itself. - David Makogon
Please share your code. - Gaurav Mantri
I have added my code and the error that is returned - David Schechter
@DavidMakogon I have tried looking up tutorials and the github repo, I cannot find anything explaining how to use the options/partitionKey and how to send it - David Schechter

2 Answers

3
votes

You will get no results if you set the partition key as the name of the partition key property because it is actually expecting the literal value for 1 of the partitions i.e. the query will be run against 1 partition only. Possibly an array of partition keys could be used but I have not tried that.

I cannot find any documentation on the partition key option in the node.js SDK but it does work. If you just want to query all partitions use:

var options = 
    {
        enableCrossPartitionQuery: true
    };
2
votes

The error message is quite clear: because your query spans multiple partitions, you need to set enableCrossPartitionQuery option to true:

client.queryDocuments(
    collectionUrl,
    'SELECT * FROM c WHERE c.akunaCustomer = "xyz"',
    { enableCrossPartitionQuery: true }
)

https://stackoverflow.com/a/44130039/1234356