Agrawal Shraddha
Agrawal Shraddha

Reputation: 764

Query int for searching in cosmos db?

We have stored following document

Document 1- { "Sample1":1, "Sample2":"SampleData" }

Document 2- { "Sample1":10, "Sample2":"Sample" }

Document 3- { "Sample1":3, "Sample2":"Sample" }

We have to query like if user searches for 1 then it should return both documents and we don't know which type of data is stored in cosmos db. How should I query this

select * from c where contains(c.Sample1, 1) 

is not returning any results and for string i have to use contains Can anyone please help me how i have to query int using contains?

Thanks, Shraddha Agrawal

Upvotes: 1

Views: 1336

Answers (2)

Sajeetharan
Sajeetharan

Reputation: 222720

Why do you have to use Contains as its not an array, you could simply write a where clause,

SELECT p
    FROM products p
    WHERE p.Sample1 = 1

EDIT:

As Jay mentioned above, you can use User Defined function. One of the other method you could use is STARTSWITH

SELECT c.id FROM c
where STARTSWITH(udf.convertToString(c.Sample1),"1")

Upvotes: 0

Jay Gong
Jay Gong

Reputation: 23792

Even though your requirements is really odd,i provide a workaround that using UDF feature in cosmos db.

Create User Defined Function:

enter image description here

function userDefinedFunction(val){
    return val.toString()
}

Then use sql:

SELECT c.id FROM c
where CONTAINS(udf.convertToString(c.Sample1),"1")

Output:

enter image description here

Upvotes: 1

Related Questions