MongoDB文档中的单引号

In my HTML page user enters a search query and send it via GET. In some of my mongodb documents there are fields that have ' in their value. But when user enters a query that contains ' no result is returned. I also used PHP addslash function without success. How should I place the query in find function?

$col->find(['word' => new MongoRegex('/^' . addslashes($term) . '/i')];

I had same problem before 2 days and i have solved it using below query.

below is the solution for me.

SELECT *
FROM tags
WHERE `name` = 'limit\\''s'
LIMIT 0 , 30

SEE HERE

Try using htmlentities instead of addslashes

$col->find(['word' => new MongoRegex('/^' . htmlentities($term, ENT_QUOTES, 'UTF-8') . '/i')];

reference: regular expression for single quote mark in mongodb

The JS shell seems to require ' be escaped as unicode \x27 (see: this answer), but the PHP driver should not require any special treatment. Consider the following example:

$m = new MongoClient();
$c = $m->test->foo;
$c->drop();
$c->insert(['x'=>"foo'bar"]);
$c->insert(['x'=>"foobar"]);
$r = $c->find(['x' => new MongoRegex("/^foo'/i")]);
var_dump(iterator_to_array($r));

This query matches only the first document, where x is foo'bar. If you run the script, you should see that document printed out.