简体   繁体   中英

How do I use a phrase with php in a mongo text search?

I've got a simple mongo query which returns the result I want -

db.properties.findOne({$text:{$search:'\"Eiffel Petit Louvre\"'}})

However I can't seem to transpose the same query into php -

$searchString = '\"Eiffel Petit Louvre\"';
$hotel = $db->properties->findOne(
        array('$text' => array('$search' => $searchString))
);

This doesn't give me any results back. What am I doing wrong? I'm guessing it's the string. I've tried escaping the backslashes (I don't think I should need to since the string uses single quotes), but that didn't work.

Hi I had the exact same problem. After long trial and error I figured out that the following works:

array('$text'=>array('$search'=>"\"$searchString\""))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM