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

399
Views
Mongodb Search geolocation with text filters and group by in a single query

enter image description here We are using Mongo database for insert jobs related data. I want to get the count according to GEO location coordinates with text search on title and description fields.

Suppose we have records

id title                   coordinates[0]       coordinates[1]
--+-----------------------+---------------------+----------------
1  PHP developer          | 97.77               | -92.99
2  Laravel developer,php  | 97.77               | -92.99
3  python                 | 97.77               | -92.99
4  Rails                  | 23.77               | -34.99
5  Python, php            | 23.77               | -34.23

User search with "PHP" (title), latitude-longitude coordinates and specific with distance 50 miles, If the distance came in first 3 records id[1,2,3] and PHP title came in first two records id[1,2] then user will get result as a count 2. How can we make a query for this in mongo database or PHP mongo database driver query? We are going to attach our database structure as an image. If you have any query let us know. Thanks in advance.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

In Mongodb/Driver/Query it can be used like this

    $queryString = [
                    [
                        '$geoNear'=> [
                        'near'=> [ 'type'=> "Point", 'coordinates'=> [-73.86, 41.07 ] ],
                        'distanceField'=> "dist.calculated",
                        'maxDistance'=> '5000',
                        'includeLocs'=> "dist.location",
                        'num'=> 10000,
                        'spherical'=> true,
                        'query' => ['title'=> ['$regex' => 'sales' ]]
                        ]
                    ],
                    ['$unwind'=> '$loc'],    
                    ['$group'=>['_id'=> '$loc', 'count'=>['$sum'=>1]]],
                    ['$project'=>['res'=>['loc'=>'$_id', 'count'=>'$count']]], 
                    ['$group'=>['_id'=>null, 'total'=>['$sum'=>1], 'data'=>['$addToSet'=>'$res']]],
                ];
     $conn = new \MongoDB\Driver\Manager("mongodb://username:password@localhost:2702/admin");
     $command = new \MongoDB\Driver\Command([
                'aggregate' => 'collection_name',
                'pipeline' =>  $queryString 
            ]);

    $result = $conn->executeCommand('mongodbname', $command);
over 4 years ago · Santiago Trujillo Report

0

Try this:

db.crawled_jobs.aggregate([{ "$geoNear": {"near": { "type": "Point", "coordinates": [  -73.86,  41.07  ]  },"spherical": true,"maxDistance": 500000,
 "distanceField": "distance","query": {"title": /php/}}}])
over 4 years ago · Santiago Trujillo Report

0

check this sardar ji :

db.hotels.find({ "$text": { "$search": "test" }, "location": { "$geoWithin": { "$centerSphere": [[ 72.867804, 19.076033 ], 5000 ] } }})

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!