Optional query parameters in Cosmos DB

mlcubed 46 Reputation points
2022-09-22T19:55:26.193+00:00

Hello, is it possible to pass optional query parameters to query cosmos DB (SQL API)? I found this question on SO but it is unanswered (https://stackoverflow.com/questions/70194575/cosmosdb-optional-query-parameters).

Thank you.

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,543 questions
0 comments No comments
{count} votes

Accepted answer
  1. GeethaThatipatri-MSFT 29,017 Reputation points Microsoft Employee
    2022-09-23T16:13:29.07+00:00

    Hi, @mlcubed Welcome to the Microsoft Q&A Platform, and thanks for your query.
    I would normally try to use the ternary operator or an expression with "OR" but have not tried to test that answer out.
    If you are writing that query in code, it may be easier to put evaluation of the optional parameter client-side to save on RUs anyways. If you are forced to do it in SQL query then you should check out the ternary operator.
    Can you do something like this? The query will change based on your data model.
    SELECT * FROM c WHERE c.region = @region AND (ARRAY_CONTAINS(c.businesses, @business) OR c.businesses = null)

    Regards
    Geetha


0 additional answers

Sort by: Most helpful