[英]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. 如何使用正则表达式匹配MongoDB中的给定输入值,因为传递的值大于DB中的值。 Any suggestion?
有什么建议吗? thanks.
谢谢。
Value in DB - 123456 Input Search Value - 1234560000
DB中的值-123456输入搜索值-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. FYI-> Studentid是String字段。
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. 从表面上看,您将必须知道inputValue可以是比文档ID更多的字符(因为我们在谈论字符串)。
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: 如果您知道您的输入可能是您想要的ID,然后是“ n”个零(根据您的示例),则您的正则表达式将更像:
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>));
我真正的建议是使用一个数字作为ID(可以是
int
或long
,这样您可以执行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. 它返回所有
Studentid
大于或等于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 免责声明 :我不确定您使用的是哪个版本的Mongo驱动程序,但是以下是我使用的文档以及我的回答基于的内容: Mongo驱动程序文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.