如何将数组作为Cosmos DB查询的SQL查询参数传递 [英] How can I pass array as a sql query param for cosmos DB query

查看:88
本文介绍了如何将数组作为Cosmos DB查询的SQL查询参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数组作为参数传递给SqlQuerySpec,以便在构建对天蓝色的db数据库的查询时能够在IN表达式中使用它. 我正在尝试做的事情就像我们对常规(字符串,整数等)参数所做的事情一样:

I want to pass array as a param to SqlQuerySpec to be able to use it in the IN expression when building query for azure cosmos db. What i'm trying to do is something like we do with regular (string, int etc) params:

private SqlQuerySpec BuildQuery(IEnumerable<string> exclTypes)
{
    var queryText = "SELECT * FROM root r WHERE r.Type NOT IN (@types)";
    var parameters = new SqlParameterCollection{new SqlParameter("@types", exclTypes.ToArray())};
    return new SqlQuerySpec()
    {QueryText = queryText, Parameters = parameters};
}

但是这种方式行不通.我还有其他方法可以将数组作为参数传递吗? 谢谢.

But that doesn't work in such way. Any other ways I can pass array as a param? Thanks.

推荐答案

您的查询应如下所示:

SELECT * FROM root r WHERE ARRAY_CONTAINS(@types, r.Type) <> true

然后您可以将@types作为数组传递,并检查该数组是否包含您在文档的属性r.Type中拥有的值.

then you can pass @types as array and check if that array contains value you have in property r.Type in your document.

裁判:

https://docs.microsoft.com/zh-CN/azure/cosmos-db/sql-api-sql-query-reference#bk_array_contains https://github.com/Azure/azure-documentdb-node/issues/156

这篇关于如何将数组作为Cosmos DB查询的SQL查询参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆