简体   繁体   中英

Not able to match Pattern Search in Mongo DB

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM