Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get simple CosmosDB query to work via Node.js - but works fine via Azure's Query Explorer

I'm using CosmosDB (node.js via documentdb api) - partitioned on 'id' only.

A simple query (SELECT * FROM c WHERE c.akunaCustomer = "xyz") works fine from Azure Query Explorer, but my node.js code gives the following error - running the identical query.

code: 400, BadRequest, 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.

Here's my code:

var documentClient = require("documentdb").DocumentClient;
var config = require("./config");

var client = new documentClient(config.endpoint, {"masterKey": config.primaryKey});

var databaseUrl = `dbs/${config.database.id}`;
var collectionUrl = `${databaseUrl}/colls/${config.collection.id}`;

client.queryDocuments(collectionUrl,
    'SELECT * FROM c WHERE c.akunaCustomer = "xyz"'
    ).toArray((err, results) => {
        if(err) {
            console.log(err);
        }
         else {
            for (var queryResult of results) {
                let resultString = JSON.stringify(queryResult);
                console.log(`Query: ${resultString}`);
            }
        }
    });

Clearly I'm missing something. :-( Any help would be appreciated.

like image 485
dieterb Avatar asked May 23 '17 04:05

dieterb


People also ask

What port does Cosmosdb use?

Service port ranges The port 10250 maps to a default Azure Cosmos DB API for MongoDB instance without geo-replication.

Which API should you use to store and query JSON documents in Azure Cosmos DB?

Quick note: Core (SQL) API is the native API in Cosmos DB and is also called SQL API. It supports a SQL-like query language that allows retrieving documents using SELECT and other basic SQL commands.


1 Answers

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 }
)
like image 66
Mikhail Shilkov Avatar answered Oct 21 '22 18:10

Mikhail Shilkov