MongoDB 反向正则表达式 [英] MongoDB reverse regex

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

问题描述

我在 MongoDB 中有一个带有正则表达式属性的文档

I have a document in MongoDB with a regex attribute

    {
        _id: ObjectId("516023189732da20ce000004"),
        regex: /^(my|your)s+regex$/
    }

我需要使用类似 db.col.find({regex: 'your regex'})db.col.find({regex: 'my正则表达式'}).在 MySQL 中,我会这样做:SELECT * FROM table WHERE 'my regex' REGEXP table.regex.我如何在 MongoDB 中实现这一点?

and I need to retrieve this document with something like db.col.find({regex: 'your regex'}) and db.col.find({regex: 'my regex'}). In MySQL I'd do: SELECT * FROM table WHERE 'my regex' REGEXP table.regex. How can I achieve this in MongoDB?

推荐答案

您可以按以下方式使用 $where 运算符:

You can use the $where operator in the following fashion:

db.col.find({$where: ""my regex".match(this.regex)"})

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

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