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.
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);
Try this:
db.crawled_jobs.aggregate([{ "$geoNear": {"near": { "type": "Point", "coordinates": [ -73.86, 41.07 ] },"spherical": true,"maxDistance": 500000,
"distanceField": "distance","query": {"title": /php/}}}])
check this sardar ji :
db.hotels.find({ "$text": { "$search": "test" }, "location": { "$geoWithin": { "$centerSphere": [[ 72.867804, 19.076033 ], 5000 ] } }})