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

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

问题描述

我在MongoDB中有一个带有regex属性的文档

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 regex'})之类的内容来检索此文档. 在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天全站免登陆