php - 如何在php中转换mongo db查询

标签 php arrays json mongodb

我们已经创建了 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/

相关文章:

php - 如何创建 Laravel Eloquent 实体的空实例

java - 无法使用spring boot连接mysql数据库错误HTTP Status 404请求的资源不可用

javascript - 使用 jQuery 解析对象的 JSON 数组

php - 如何在 Laravel 5.2 中删除重复的行

php - 使用 php 保存可编辑表中的值

javascript - 如何在 Javascript 对象数组中进行搜索?

c# - 为什么我的C#程序无法识别长度?

php - 可以列出所有 PHP 类及其方法和属性吗?

php - 发送 SOAP 1.2

javascript - 提取任意给定数组中的所有数字,然后将它们全部加在一起。