1

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:

Mongodb Nodejs Regex Query

2
  • 1
    Use it without the quotes. Commented Jan 14, 2018 at 3:55
  • @AakashVerma Exactly.... How could I miss this -_-. Would you like to post your comment as an answer so that I can accept it? Commented Jan 14, 2018 at 3:57

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this hope it will works.

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

1 Comment

This pattern matches only the strings starting with "cck".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.