How to Pass Parameters to Cosmosasynccontainer

I am using the method queryItems from CosmosAsyncContainer, as shown here:

(t)queryitems(java-lang-string-com-azure-cosmos-models-cosmosqueryrequestoptions-java-lang-class(t))

So far I have been able to perform a query by concatenating the query String with a parameter, like

String query = QUERY + "\"" + value + "\"";

I would like to avoid this concatenation and pass the parameter in a different way. Maybe by changing the CosmosQueryRequestOptions, for now I use them like this: CosmosQueryRequestOptions queryOptions = new CosmosQueryRequestOptions();

Any help will be appreciated

0

1 Answer

Instead of creating the query by using string concatenation, you can make use of SqlQuerySpec(String queryText, List<SqlParameter> parameters) which lets you create a parameterized query.

Something like:

SqlQuerySpec spec = new SqlQuerySpec(
           "SELECT * FROM c WHERE c.id = @id",
           new SqlParameterCollection(new SqlParameter("@id", "id-value")));

You can then use the following override of the queryItems method: <T>queryItems(SqlQuerySpec querySpec, CosmosQueryRequestOptions options, Class<T> classType).

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest