Home > database >  When using the $regex text matching strange problems
When using the $regex text matching strange problems

Time:10-01

use $regex, we can use the following three grammar:

{& lt; Field> : {$regex:/pattern/, $options: '& lt; Options> '}}
{& lt; Field> : {$regex: 'pattern', $options: '& lt; Options> '}}
{& lt; Field> : {$regex:/pattern/& lt; Options> }}

compares the two kinds of grammar tests before we got a completely different effect after
13 px] [size=
[/size]
A collection of # test
Pradb> Db. Newtab. The find ()
{" _id ": ObjectId (" 5 a2a2555c8ba4994e594397f"), "f1" : "realapp"}
{" _id ": ObjectId (" 5 a2a255dc8ba4994e5943980"), "f1" : "racapp"}
{" _id ": ObjectId (" 5 a2a2562c8ba4994e5943981"), "f1" : "mysql"}
{" _id ": ObjectId (" 5 a2a2d60c8ba4994e5943982"), "f1" : "vcapp"}
{" _id ": ObjectId (" 5 a2a340fc8ba4994e5943983"), "f1" : "rea \ napp"}
{" _id ": ObjectId (" 5 a2e4156c3b9a66ec4988f15"), "f1" : "suretrea"}

# using single quotes will string enclosed, we get the query result is "realapp
"Pradb> Db. Newtab. Find ({f1: {$regex: 'rea \ n# comment 1 \ nl', $options: "x"}})
{" _id ": ObjectId (" 5 a2a2555c8ba4994e594397f"), "f1" : "realapp"}

# use/string enclosed, we get the query result is "rea \ napp
"Pradb> Db. Newtab. Find ({f1: {$regex/rea \ n# : 1 \ nl/comment, $options: "x"}})
{" _id ": ObjectId (" 5 a2a340fc8ba4994e5943983"), "f1" : "rea \ napp"}

//and "' why would lead to different results output?

  • Related