Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass array as a sql query param for cosmos DB query

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.

like image 461
Olha Shumeliuk Avatar asked Dec 28 '17 07:12

Olha Shumeliuk


People also ask

How do I query Cosmos SQL?

In the Azure Cosmos DB blade, locate and select the Data Explorer link on the left side of the blade. In the Data Explorer section, expand the NutritionDatabase database node and then expand the FoodCollection container node. Within the FoodCollection node, select the Items link. View the items within the container.

Can you use SQL in Cosmos DB?

The Azure Cosmos DB API for NoSQL supports querying documents using SQL.


1 Answers

Your query should look something like this:

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

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

refs:

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

like image 165
dee zg Avatar answered Sep 28 '22 03:09

dee zg