2

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...

5
  • You can convert the data into your db to store real number instead of string, is that a possibility? Commented Sep 19, 2022 at 9:08
  • hi @CharchitKapoor that is the "nuclear" option. we're trying to avoid that Commented Sep 19, 2022 at 9:11
  • Ok, so at present, you have strings and you are applying filters, by passing strings is that accurate? Commented Sep 19, 2022 at 9:20
  • correct, if i pass in an integer the DB/filter will not match anything Commented Sep 19, 2022 at 9:40
  • Then 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 parameters Commented Sep 19, 2022 at 9:46

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.