Search code examples
mongodbmongodb-querydoctrine-odm

Doctrine Mongo search


On Mongo DB manual page, it says, "to match on a phrase, as opposed to individual terms, enclose the phrase in escaped double quotes (\")"

"\"ssl certificate\""

How can I do this with Doctrine query builder? At moment, I do query like below:

$name = '\"'.$name.'\"';
$qb->field('entityName')->text($name)->getQuery()->toArray();

above Doctrine query returns empty [], query used by Doctrine as below:

array:5 [
    "type" => 1
    "select" => array:1 [
        "entityName" => 1
    ]
    "limit" => 10
    "query" => array:1 [
        "$text" => array:1 [
        "$search" => "\"oxford tru\""
        ]
    ]
    "newObj" => []
]

if I do the query in mongo shell,

db.collection.find({$text: {$search: "\"oxford tru\""}},{entityName:1})

above mongo shell command returns 5 results


Solution

  • Try $name = '"'.$name.'"'; There's no need to escape " in ' (contrary to mongo shell where you escape quotes within ")