带有 IN 语句的 DocumentDB SP

DocumentDB SP with IN statement

我的DocumentDB SP:

function getSetOfProductDetails(productIds) {
    var collection = getContext().getCollection();

    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        "SELECT * FROM product WHERE product.productId IN ("+ productIds +")" ,
        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) getContext().getResponse().setBody(feed);
        });

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

我的 C# 代码:

string productIdList = "'271762','288738','235521','266714'";
 List<Product> productList = new List<Product>();
 try
 {
     productList = await  DocumentDBRepository<List<Product>>.ExecuteStoredProc("product","getSetOfProductDetails", productIdList );
}

DocumentDBRepository 代码:

public static async Task<T> ExecuteStoredProc(string collectionId, string storedProcedureId, params object[] parameter)
        {
            ConnectToDatabase();
            StoredProcedureResponse<T> document = await _documentClient.ExecuteStoredProcedureAsync<T>(UriFactory.CreateStoredProcedureUri(_databaseId, collectionId, storedProcedureId), parameter);
            return (T)(dynamic)document;
        }

我在productList中收到的结果集是空白的,而在Collection中有文档。这是为 DocumentDB 执行 IN 语句的正确方法吗?

如果 SELECT * FROM product WHERE product.productId IN ("+ productIds +") 可以获得正确的值,则应该与您的代码一起使用。我用你的代码进行了测试,它在我这边工作正常。

正如您提到的文档正在收集中,请尝试通过以下方式进行故障排除:

1.Make 确定 SQL 可以得到预期的结果。并且字段区分大小写。在我的产品模型中,我使用了字段 ProductId,所以在我的例子中,我使用 product.ProductId 而不是 product.productId 来查询.我用 Azure 门户查询它

2.Execute 来自 Azure 门户的存储过程

如果两者都有效,请再次尝试您的代码。