简体   繁体   English

无法匹配Mongo DB中的模式搜索

[英]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(可以是intlong ,这样您可以执行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.

相关问题 在mongo db中搜索复杂的文档 - Search in complex documents in mongo db 从eclipse运行的程序能够将数据插入到本地机器的Mongo DB中,而不能插入到远程机器的Mongo DB中 - Program run from eclipse is able to insert data into Mongo DB of local machine but not into Mongo DB of remote machine 无法将静态字段从Java更新为Mongo DB - Not able to update a static field from java to mongo db 无法使用Morphia从Mongo数据库中提取 - 没有可用的构造函数 - Not able to pull from Mongo db using Morphia - No usable constructor 无法使用Java执行mongo db聚合查询, - Not able to execute the mongo db aggregate query using java, 无法使用MongoRepository从Spring在Mongo DB中保存值 - Not able to save value In Mongo DB from Spring using MongoRepository 从java到mongo db的数据访问的任何推荐模式或策略? - Any recommend pattern or strategies for data access from java to mongo db? 如何在具有$ match和$ in的Mongo db Java驱动程序中使用聚合? - How to use aggregation in Mongo db Java driver with $match and $in? 在Mongo DB ISODate上进行搜索未使用Java获取任何记录 - Search on Mongo DB ISODate is not fetching any record using java 无法在基于电子商务的微服务中使用 Mongo Db 进行一对多关系 - Not Able to use Mongo Db for one to many relation in my ecommerce based microservice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM