用CONTAINS查询存储过程中转换为字符串的数字不起作用

时间:2019-05-09 13:35:17

标签: azure azure-cosmosdb

我尝试在cosmos db中写一个存储过程来执行查询,该查询根据2个属性返回一些对象:字符串和整数。该过程采用的参数必须是字符串,但是第二个属性是文档中的int。

查询在过程外执行时效果很好,但在过程中每次都返回一个空数组。

我试图用一个简单的查询来创建过程: 'SELECT * FROM listeLignes l',但每次也会返回一个空数组。

实际上,即使生成的存储过程示例也不起作用,并返回空结果。

集合中没有很多对象。

function LigneVoiesByCodeLigneOrText(libelle, codeLigne) {
    var collection = getContext().getCollection();

    var query = 'SELECT * FROM l WHERE 1=1 ';

    if(libelle != null){
        query = query + 'AND CONTAINS(l.libelle, "'+ libelle +'") ';
    }

    if(codeLigne != null){
       query = query + 'AND CONTAINS(toString(l.codeLigne), "'+ codeLigne +'") ';
    }

    console.log(query);

    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        query,
        function (err, feed, options) {
            if (err) throw err;

            if (!feed || !feed.length) {
                var response = getContext().getResponse();
                response.setBody('no docs found');
            } else {
                var response = getContext().getResponse();
                var body = { feed : feed[0] };
                response.setBody(JSON.stringify(body));
            }
        });

        if (!isAccepted) throw new Error('The query was not accepted by the server.');
}

1 个答案:

答案 0 :(得分:0)

既然您说过There is not a lots of objects in the collection,就可以在cosmos db中使用udf在SQL中解决问题。

udf:

enter image description here

存储过程: (请注意CONTAINS(udf.bbb(l.countyid),请注意)

// SAMPLE STORED PROCEDURE
function sample(libelle,codeLigne) {
    var collection = getContext().getCollection();

    var query = 'SELECT l.id,l.countyid FROM l WHERE 1=1 ';

    if(libelle != null){
        query = query + 'AND CONTAINS(l.id, "'+ libelle +'") ';
    }

    if(codeLigne != null){
       query = query + 'AND CONTAINS(udf.bbb(l.countyid), "'+ codeLigne +'") ';
    }

    console.log(query);

    // Query documents and take 1st item.
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        query,
    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();
            response.setBody(feed);
        }
    });

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}