Reputation: 494
How to match the given input value in MongoDB using regex, Since the value that was passed is greater than the value in the DB. Any suggestion? thanks.
Value in DB - 123456 Input Search Value - 1234560000
I have tried given below query ,but its not returning any result.
db.getCollection('STUDENT').find(
{ Studentid:{$regex : '1234560000'}});
FYI-> Studentid is String field.
Upvotes: 0
Views: 171
Reputation: 53
From what it sounds like, you will have to know that the inputValue can be more characters (since we are talking strings) than your documents' IDs.
If you know that your input could be the ID you want followed by 'n' number of zero's (based on your example) then your regex would look more like:
inputValue = inputValue.replace("0", "");
db.getCollection('STUDENT').find( { Studentid:{$regex : inputValue + '[0]*'}});
My real suggestion is to use a number for the ID (be it int
or long
, that way you can do db.getCollection('STUDENT').find(eq("Sudentid", <inputValue>));
or if you're looking for multiple students
db.getCollection('STUDENT').find(gte("Sudentid", <inputValue>));
which returns all documents with Studentid
greater than or equal to the inputValue.
Disclaimer: I'm not sure which version of the Mongo-driver you're using, but here are the docs I used and what my answer is based on: Mongo Driver Docs
Upvotes: 1