Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

308
Views
how to convert mongo db query in php

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)

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Fix it with this

'query' => ['title'=> array('$regex' => 'NYC')]
over 4 years ago · Santiago Trujillo Report

0

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

'query' => ['title' => new MongoDB\BSON\Regex('sales')]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!