javamongodbmongodb-querymongodb-javaazure-cosmosdb-mongoapi

MongoDB Java Driver Filter Integer Value of a String


I have MongoDB Document parameter of type "real" number but is represented in the DB as a string- e.g "cId"="200" instead of "cId"=200I have to query the DB for a range of numbers; e.g. filter equivalent to SELECT ALL where cId is less than 10 and greater than 5.

Is there a way to query the integer value of this parameter using a similar functional equivalent to Integer.parseInt(str); for example

My filter looks like

Bson filter = Filters.and(Filters.gt("cId", "0"), Filters.lt("cId","4"));

I'm hoping for an equivalent of something like

Bson filter = Filters.and(Filters.gt("cId", valueOf("0")), Filters.lt("cId",valueOf("4")));

Thanks...


Solution

  • Posting @CharchitKapoor comment above as the best available answer within the limitations of my question.... (however in my case, I ended up converting the data on insertion to a real number)

    The best thing is to write an aggregation pipeline, convert your cId to a number within the pipeline using $convert mongodb.com/docs/manual/reference/operator/aggregation/convert and then match it using your parameter