Azure cosmosDB 默认存储过程不起作用
Azure cosmosDB default stored procedure did not work
我在创建存储过程时,这是Azure默认提供的示例代码。我的容器(学生)非常简单,只有 2 个项目。分区键是id.
{name: "aaa", id: "1"}
{name: "bbb", id: "2"}
这是 Azure CosmosDB 提供的示例存储过程代码。
// SAMPLE STORED PROCEDURE
function sample(prefix) {
var collection = getContext().getCollection();
// Query documents and take 1st item.
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
'SELECT * FROM root r',
function (err, feed, options) {
if (err) throw err;
// Check the feed and if empty, set the body to 'no docs found',
// else take 1st element from feed
if (!feed || !feed.length) {
var response = getContext().getResponse();
response.setBody('no docs found');
}
else {
var response = getContext().getResponse();
var body = { prefix: prefix, feed: feed[0] };
response.setBody(JSON.stringify(body));
}
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
为什么总是return“找不到文档”?我尝试了不同的 sql 查询,例如“select * 来自学生”或“select * 来自 root”或“select * 来自 c”,但 none他们工作。
执行存储过程时,需要传递partition key的值。在您的情况下,您需要像这样传递“1”或“2”作为分区键的值(不是“id”):
我在创建存储过程时,这是Azure默认提供的示例代码。我的容器(学生)非常简单,只有 2 个项目。分区键是id.
{name: "aaa", id: "1"}
{name: "bbb", id: "2"}
这是 Azure CosmosDB 提供的示例存储过程代码。
// SAMPLE STORED PROCEDURE
function sample(prefix) {
var collection = getContext().getCollection();
// Query documents and take 1st item.
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
'SELECT * FROM root r',
function (err, feed, options) {
if (err) throw err;
// Check the feed and if empty, set the body to 'no docs found',
// else take 1st element from feed
if (!feed || !feed.length) {
var response = getContext().getResponse();
response.setBody('no docs found');
}
else {
var response = getContext().getResponse();
var body = { prefix: prefix, feed: feed[0] };
response.setBody(JSON.stringify(body));
}
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
为什么总是return“找不到文档”?我尝试了不同的 sql 查询,例如“select * 来自学生”或“select * 来自 root”或“select * 来自 c”,但 none他们工作。
执行存储过程时,需要传递partition key的值。在您的情况下,您需要像这样传递“1”或“2”作为分区键的值(不是“id”):