一个非常简单的问题。我的index.js文件中有以下功能。
const databaseId = config.database.id;
const collectionId = config.collection.id;
const client = new CosmosClient({ endpoint: CosmosDBName, auth: { masterKey: CosmosDBKey } });
const query = `SELECT c.message.id FROM ${collectionId} c WHERE c.message.timestampFormatted > '${start}' AND c.message.timestampFormatted < '${end}'`;
queryCosmosDB(client, databaseId, collectionId, query)
async function queryCosmosDB(client, databaseId, collectionId, query, context) {
const querySpec = {
query: query,
parameters: []
};
const { result: results } = await client.database(databaseId).container(collectionId).items.query(querySpec).toArray();
for (var queryResult of results) {
let resultString = JSON.stringify(queryResult);
context.log(`\tQuery returned ${resultString}\n`);
}
}
如果执行此功能,响应将是以下错误(我省略了一些无用的信息)
ERROR: {"code":400,"body":"{\"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. ...}
现在,我获得了CosmosDB使用的分区架构,但是就我而言,最好的办法是什么?我应该以某种方式更改查询吗?如果必须插入某种跨分区查询字符串,该怎么办?
我发现的与此有关的唯一另一个问题是this SO's question,但是我不像他那样管理数据,因为我认为SDK一年半以前已经发生了很大变化。我目前使用this tutorial作为基准。
非常感谢您的帮助!
答案 0 :(得分:1)
您可以通过使用NodeJS sdk作为query
功能级别选项的一部分来简单地启用跨分区查询。
async function queryCosmosDB(client, databaseId, collectionId, query, context) {
const querySpec = {
query: query,
parameters: []
};
const options = {
enableCrossPartitionQuery: true
};
const { result: results } = await client.database(databaseId).container(collectionId).items.query(querySpec, options).toArray();
for (var queryResult of results) {
let resultString = JSON.stringify(queryResult);
context.log(`\tQuery returned ${resultString}\n`);
}
}