簡體   English   中英

如何在php中轉換mongo db查詢

[英]how to convert mongo db query in php

我們已經創建了 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\\/"} But we need like this "query": { "title": /sales/ }添加了反斜杠"query":{"title":"\\/sales\\/"} But we need like this "query": { "title": /sales/ }

誰能幫助我們

\\MongoDB\\Driver\\Command不接受字符串 :( 它需要唯一的不是字符串的數組)

用這個修復它

'query' => ['title'=> array('$regex' => 'NYC')]

您需要使用MongoDB\\BSON\\Regex類來生成正則表達式如下:

'query' => ['title' => new MongoDB\BSON\Regex('sales')]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM