我们已经创建了 MongoDB 查询,但它没有转换为 PHP
MongoDB 查询
db.crawled_jobs.aggregate(
[
{
$geoNear: {
near: {
type: "Point",
coordinates: [-73.86, 41.07 ]
},
distanceField:"dist.calculated",
maxDistance: 100000,
includeLocs: "dist.location",
num: 1225,
spherical: true,
"query": { "title": /sales/ }
}
}
])
Mongodb 查询工作正常,我们得到结果
在 php \MongoDB\Driver\Command
在 PHP 中创建一个数组并用于 MongoDB 查询
$queryString = [['$geoNear'=> ['near'=> [ 'type'=> "Point",'coordinates'=> [$lon,$lat] ], 'distanceField'=> "dist.calculated",'maxDistance'=> $maxDistance, 'includeLocs'=> "dist.location", 'num'=> 10000, 'spherical'=> true, 'query' => ['title' => '/sales/'] ] ] ];
在此查询之后,MongoDB 查询如下所示
db.crawled_jobs.aggregate([{"$geoNear":"near":"type":"Point","coordinates":[-73.86,41.07]},"distanceField":"dist.calculated","maxDistance":100000,"includeLocs":"dist.location","num":1225,"spherical":true,"query":{"title":"\/sales\/"}}}])
我们没有得到结果,因为它在查询中添加了反斜杠
"query":{"title":"\/sales\/"}
但我们需要像这样的“query”:{“title”:/sales/}
谁能帮助我们
\MongoDB\Driver\Command
不接受字符串:(它需要唯一的数组而不是字符串)
最佳答案
用这个修复它
'query' => ['title'=> array('$regex' => 'NYC')]
关于php - 如何在php中转换mongo db查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41824083/