2

I am tying to query an existing Azure CosmosDB database with NodeJS.

getItems: function (callback) {
    var self = this;

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

    self.client.queryDocuments(self.collection._self, querySpec).toArray(function (err, results) {
        if (err) {
            console.log(err.body);
            callback(err);
        } else {
            callback(null, results);
        }
    });
}

For some reason it is keep complaining about Cross Partition Query. I am not really sure what it is. Any ideas where I may find this Partition Key and how to set it? Also, how can I avoid this exception in the query?

Full error 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.

P.S. I know there are few similar questions asked already, but none of them addresses with NodeJS.

1 Answer 1

2

You're getting this error because the document collection you're querying against is a partitioned collection.

In order to query against a partitioned collection, you would either need to specify a partition against which to execute the query or specify that you want to query across partitions. BTW, former is recommended.

For cross-partition queries, you would need to specify the same in options. So your code would be:

getItems: function (callback) {
    var self = this;

    var querySpec = {
        query: 'SELECT * FROM root'
    };
    const options = {//Query options
      enableCrossPartitionQuery: true
    };

    self.client.queryDocuments(self.collection._self, querySpec, options).toArray(function (err, results) {
        if (err) {
            console.log(err.body);
            callback(err);
        } else {
            callback(null, results);
        }
    });
}

If you're querying against a single partition, you will need to specify the partition key value in the query. In this case, your options would look something like:

const options = {
  partitionKey: 'partition-key-value'
};
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply. Something really strange happens when I add const options. I am running it in VS Code debugger and adding options in queryDocuments stops the execution before it gets to toArray callback function. Does that make sense?
it just stop the execution without giving any results or error.
Can you try changing const keyword to var? I was using ES6.
I tried with var as well. It doesn't make any difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.