Reputation: 4094
I'm aware, there are many similar questions related to mongodb regex, including:
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).
What I have try
In short, this one works:
> db.web_configs.find({key: 'cck'}).pretty();
But, this one:
> db.web_configs.find({key: "/cck/"}).pretty();
and this one:
> db.web_configs.find({key: {$regex:"/cck/"}}).pretty();
doesn't work.
I try mongodb in my own machine. So, I guess the problem might be the outdated version of mongodb or the wrong regex pattern.
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?
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();
>
EDIT: 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();
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:
Upvotes: 1
Views: 4128
Reputation: 3994
Perhaps, you had forgotten about not putting regex in single or double quotes.
These are few of the ways you can use regex for your query.
> db.web_configs.find({key: /cck/}).pretty();
or
> db.web_configs.find({key: /^cck$/}).pretty();
or
> db.web_configs.find({key: {$regex: "cck"}).pretty();
or, the one from your edits,
> db.web_configs.find({key: {$regex:/cck/}}).pretty();
Hope the difference between with respect to quotes and slashes in the last two statements where $regex
are used is clear.
Upvotes: 3
Reputation: 1718
Try this hope it will works.
db.web_configs..find({"key" : /^cck/}).pretty();
Upvotes: 0