mongodb 正则表达式不起作用 [英] mongodb regex doesn't work

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

问题描述

我知道,有很多与 mongodb regex 相关的类似问题,包括:

I'm aware, there are many similar questions related to mongodb regex, including:

在第一个问题中,据说 try.mongodb.com 有一个错误,使正则表达式不起作用.

In the first question, it was said that try.mongodb.com has a bug that make regex doesn't work.

在第二个问题和许多其他问题中,问题与错误的正则表达式格式有关.

In the second question, and many other question, the problem was related to wrong regex format.

我看到了问题,似乎我的问题有点不同(或者我可能会在这里遗漏一些东西).

I have see the questions, and seemingly my problem is a bit different (or I might miss something here).

我的尝试

简而言之,这个有效:

> db.web_configs.find({key: 'cck'}).pretty();

但是,这个:

> db.web_configs.find({key: "/cck/"}).pretty();

还有这个:

> db.web_configs.find({key: {$regex:"/cck/"}}).pretty();

不起作用.

我在自己的机器上尝试使用 mongodb.所以,我猜问题可能是 mongodb 版本过时或错误的正则表达式模式.

I try mongodb in my own machine. So, I guess the problem might be the outdated version of mongodb or the wrong regex pattern.

我的mongodb版本是3.4.7,最新的是3.6.但我不确定这是问题所在.

My mongodb version is 3.4.7, while the newest is 3.6. But I'm not sure this is the problem.

有人知道这有什么问题吗?

Does anyone has any clue what could be wrong with this?

此版本的 mongodb 是通过 apt 通过 ubuntu 存储库下载的.因此,除非有非常好的更新理由,否则我更愿意使用当前版本.

This version of mongodb was downloaded via ubuntu repository via apt. So unless there is a really good reason to update, I prefer to stay with this current version.

gofrendi@asgard:~$ mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Server has startup warnings: 
2018-01-14T08:23:46.606+0700 I STORAGE  [initandlisten] 
2018-01-14T08:23:46.606+0700 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-01-14T08:23:46.606+0700 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-01-14T08:23:52.532+0700 I CONTROL  [initandlisten] 
2018-01-14T08:23:52.532+0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-01-14T08:23:52.532+0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-01-14T08:23:52.532+0700 I CONTROL  [initandlisten] 
> db.web_configs.find({key: 'cck'}).pretty();
> use chimera-web-app;
switched to db chimera-web-app
> db.web_configs.find({key: 'cck'}).pretty();
{
    "_id" : ObjectId("5a58bfc2516018595c8e6913"),
    "key" : "cck",
    "defaultConfig" : 1,
    "value" : "someValue"
}
> db.web_configs.find({key: "/cck/"}).pretty();
> db.web_configs.find({key: {$regex:"/cck/"}}).pretty();
> 

这个问题已经解决了(看评论).简而言之,这一项有效:

This problem has been already solved (look at the comment). In short, these one work:

> db.web_configs.find({key: /cck/}).pretty();
> db.web_configs.find({key: {$regex:/cck/}}).pretty();

另外,如果你使用 NodeJs,如果你不知道表达式是否应该是 regex 对象,而不是字符串,你很可能会遇到同样的问题.

Also, if you use NodeJs, it is a big chance you will encounter the same problem if you are not aware if the expression should be regex object, not a string.

这个问题与那个问题有关:

This question is related to that problem:

Mongodb Nodejs 正则表达式查询

推荐答案

也许,您忘记了不要将正则表达式放在单引号或双引号中.

这些是您可以使用正则表达式进行查询的几种方式.

These are few of the ways you can use regex for your query.

> db.web_configs.find({key: /cck/}).pretty();

> db.web_configs.find({key: /^cck$/}).pretty();

> db.web_configs.find({key: {$regex: "cck"}).pretty();

或者,你编辑的那个,

> db.web_configs.find({key: {$regex:/cck/}}).pretty();

希望在使用 $regex 的最后两个语句中引号和斜杠之间的区别是清楚的.

Hope the difference between with respect to quotes and slashes in the last two statements where $regex are used is clear.

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

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