我有一个Cosmos DB存储过程,我在其中传递逗号分隔的ID列表。我需要将这些ID传递给查询。当我将一个值传递给参数时,它的工作正常,但不会超过一个值。
如果有人能在这里帮忙的话会很棒。 下面是存储过程的代码:
function getData(ids) {
var context = getContext();
var coll = context.getCollection();
var link = coll.getSelfLink();
var response = context.getResponse();
var query = {query: "SELECT * FROM c where c.vin IN (@ids)", parameters:
[{name: "@ids", value: ids}]};
var requestOptions = {
pageSize: 500
};
var run = coll.queryDocuments(link, query, requestOptions, callback);
function callback(err, docs) {
if (err) throw err;
if (!docs || !docs.length) response.setBody(null);
else {
response.setBody(JSON.stringify(docs));
}
}
if (!run) throw new Error('Unable to retrieve the requested information.');
}
答案 0 :(得分:0)
请参阅我的示例js代码,它适用于我。
function sample(ids) {
var collection = getContext().getCollection();
var query = 'SELECT * FROM c where c.id IN ('+ ids +')'
console.log(query);
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
query,
function (err, feed, options) {
if (err) throw err;
if (!feed || !feed.length) getContext().getResponse().setBody('no docs found');
else {
for(var i = 0;i<feed.length;i++){
var doc = feed[i];
doc.name = 'a';
collection.replaceDocument(doc._self,doc,function(err) {
if (err) throw err;
});
}
getContext().getResponse().setBody(JSON.stringify("success"));
}
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
参数:'1','2','3'
希望它对你有所帮助。
答案 1 :(得分:0)
这是你可以做到的:
存储过程内部
这是参数值的显示方式:“abc,def,ghi,jkl” 如果您打算使用它,请将“stringProperty”替换为您要查询的属性的名称。
// SAMPLE STORED PROCEDURE
function spArrayTest(arrayParameter) {
var collection = getContext().getCollection();
var stringArray = arrayParameter.split(",");
var qParams = [];
var qIn = "";
for(var i=0; i<stringArray.length; i++){
var nm = '@p'+ i; // parameter name
qParams.push({name: nm, value: stringArray[i]});
qIn += ( nm +','); // parameter name for query
}
qIn = qIn.substring(0,qIn.length-1); // remove last comma
// qIn only contains a list of the names in qParams
var qDef = 'SELECT * from documents d where d.stringProperty in ( ' + qIn + ' )';
console.log(qParams[0].name);
// Query Definition to be passed into "queryDocuments" function
var q = {
query: qDef,
parameters: qParams
};
// Query documents
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
q,
function (err, feed, options) {
if (err) throw err;
// Check the feed and if empty, set the body to 'no docs found',
// else return all documents from feed
if (!feed || !feed.length) {
var response = getContext().getResponse();
response.setBody('no docs found with an stringProperty in ' + arrayParameter);
}
else {
var response = getContext().getResponse();
response.setBody(feed);
}
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
答案 2 :(得分:0)
对于数组,您应该使用ARRAY_CONTAINS
函数:
var query = {
query: "SELECT * FROM c where ARRAY_CONTAINS(@ids, c.vin)",
parameters: [{name: "@ids", value: ids}]
};
另外,如this doc中所述,您的@ids
数组也可能作为字符串发送
在Azure门户中定义存储过程时,输入参数始终作为字符串发送到存储过程。即使您将字符串数组作为输入传递,该数组也将转换为字符串并发送到存储过程。要解决此问题,您可以在存储过程中定义一个函数,以将字符串解析为数组
因此,您可能需要在查询之前对其进行解析:
function getData(ids) {
arr = JSON.parse(ids);
}
相关:
How can I pass array as a sql query param for cosmos DB query