MongoDB 正则表达式搜索整数值 [英] MongoDB Regex Search on Integer Value

查看:36
本文介绍了MongoDB 正则表达式搜索整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用正则表达式在 MongoDB 中搜索一个整数值.这可能吗?

I want to regex search an integer value in MongoDB. Is this possible?

我正在构建一个 CRUD 类型的接口,它允许在各个字段上使用 * 通配符.我正在尝试使一些整数字段的 UI 保持一致.

I'm building a CRUD type interface that allows * for wildcards on the various fields. I'm trying to keep the UI consistent for a few fields that are integers.

考虑:

> db.seDemo.insert({ "example" : 1234 });
> db.seDemo.find({ "example" : 1234 });
{ "_id" : ObjectId("4bfc2bfea2004adae015220a"), "example" : 1234 }
> db.seDemo.find({ "example" : /^123.*/ });
> 

如您所见,我插入了一个对象,我可以通过值找到它.如果我尝试一个简单的正则表达式,我实际上找不到对象.

As you can see, I insert an object and I'm able to find it by the value. If I try a simple regex, I can't actually find the object.

谢谢!

推荐答案

如果您想对数字进行模式匹配,在 mongo 中实现的方法是使用 $where 表达式并传入模式匹配.

If you are wanting to do a pattern match on numbers, the way to do it in mongo is use the $where expression and pass in a pattern match.

> db.test.find({ $where: "/^123.*/.test(this.example)" })
{ "_id" : ObjectId("4bfc3187fec861325f34b132"), "example" : 1234 }

这篇关于MongoDB 正则表达式搜索整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆