1

I'm using MongoDB with Sails.

db.post.find( { body:  {"$regex" : /^.*twitter.*$/i }}).

This query is supposed to find only posts which contain 'twitter' in their body-field. For some reason it won't find a match if a backslash is present in the field (like a newline).

Is this known? What can I do about it?

Here are two examples to showcase the problem:

This document is returned by the find-command above:

{ 
    "_id" : ObjectId("54e0d7eac2280519ac14cfda"), 
    "title" : "Cool Post", 
    "body" : "Twitter Twitter twitter twittor"
}

This document is not returned by the find-command above:

{ 
    "_id" : ObjectId("54e0d7eac2280519ac14cfdb"), 
    "title" : "Cool Post with Linebreaks", 
    "body" : "This is a cool post with Twitter mentioned into it \n Foo bar"
}
BatScream
  • 19,260
  • 4
  • 52
  • 68
Hedge
  • 16,142
  • 42
  • 141
  • 246
  • I just added two documents, one which is found currently and one which won't be found (because it contains \n – Hedge Feb 15 '15 at 18:57
  • If you just want to find anywhere in the string, instead of adding begin and end of line matches (`^.*` and `.*$`) you can simplify your regex to: `db.post.find( { body: {"$regex" : /twitter/i }})`. Note that a case-insensitive regex query isn't going to be overly efficient; best case will be a full index scan if there is a suitable one to use. – Stennie Feb 16 '15 at 04:06

1 Answers1

2

Is this known?

No. Its not an issue with the MongoDb search Engine.

What can I do about it?

You could change your Regular Expression to:

var regex = new RegExp("twitter","i");
db.posts.find( { body:  regex});

The problem with your code is the decimal point .

According to the doc,

.

(The decimal point) matches any single character except the newline character.

which is why the string with a newline character is not taken to be a match.

BatScream
  • 19,260
  • 4
  • 52
  • 68
  • Since I'm using the framework Sails.js I added the 'm' modifier to their query and submitted a pull request. Let's see whether they like it. Thanks nonetheless. – Hedge Feb 15 '15 at 19:13
  • 2
    @Hedge, I have updated my answer, to indicate why your query Regular Expression doesn't work. – BatScream Feb 15 '15 at 19:22