Search code examples
intazure-cosmosdbcontains

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


Solution

  • 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