2

We have created MongoDB query but it's not converted into PHP

MongoDB query

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 query working fine and we get results

In php \MongoDB\Driver\Command

Create an array in PHP and use for MongoDB query

$queryString = [['$geoNear'=> ['near'=> [ 'type'=> "Point",'coordinates'=> [$lon,$lat] ], 'distanceField'=> "dist.calculated",'maxDistance'=> $maxDistance, 'includeLocs'=> "dist.location", 'num'=> 10000, 'spherical'=> true, 'query' => ['title' => '/sales/'] ] ] ];

after this query, MongoDB query look like this

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\/"}}}])

We didn't get result because it add backslash in query "query":{"title":"\/sales\/"} But we need like this "query": { "title": /sales/ }

Can anyone help us

\MongoDB\Driver\Command does not accept string :( it require the only array not string)

2 Answers 2

2

Fix it with this

'query' => ['title'=> array('$regex' => 'NYC')]
Sign up to request clarification or add additional context in comments.

Comments

1

You need to use MongoDB\BSON\Regex class to generate regex as following:

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

3 Comments

MongoDB\BSON\Regex::__construct() expects exactly 2 parameters, 1 given
after use this i get this "query":{"title":{}}
What is yours php -v | head -1 and php -i | grep Mongo ? I tested it only with 5.6 + 1.2.3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.