简体   繁体   English

使用 NodeJS 查询 CosmosDB

[英]Query CosmosDB with NodeJS

I am tying to query an existing Azure CosmosDB database with NodeJS.我想使用 NodeJS 查询现有的 Azure CosmosDB 数据库。

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 .出于某种原因,它一直在抱怨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?我可以在哪里找到这个Partition Key以及如何设置它的任何想法? 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.完整错误消息:需要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. 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.

PS I know there are few similar questions asked already, but none of them addresses with NodeJS. PS 我知道已经很少有人问过类似的问题,但没有一个是用 NodeJS 解决的。

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:在这种情况下,您的options将类似于:

const options = {
  partitionKey: 'partition-key-value'
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM