How to Integrate Feature that Traces Nearby Users/Objects from Particular Location Using MongoDB and PHP?


MongoDB is an open source, cross-platform, document-oriented database, providing high availability, performance, and easy scalability. MongoDB categorized into the group of documents-oriented NoSQL databases. It works on the concept of collection and document.

Collection: A collection is a group of MongoDB documents, which are an equal of an RDBMS table. Developers can get a collection within a single database. It doesn’t implement a scheme. Generally, all documents are of related purpose and have different fields within a collection.

Document: A Document is a collection of key-value pairs. Documents have a dynamic schema that means documents in the same collection don’t require the same set of structure or fields. And, common fields in a collection’s documents may have diverse data types.

MongoDB supports 32-bit and 64-bit architectures and runs on most platforms. This document-oriented database is available as a binary, or as a package. This post will cover how to integrate a feature to trace nearby user/object from a particular location by using MongoDB and PHP.

Below coding helps developers to trace people or things. This feature, especially required to integrate into on-demand applications, where users want to search nearby taxi/objects/person.


1.Create a PHP file, main.php and put the below code

$dbname = 'Database Name'; // Database Name
$m = new Mongo(); // connect
$db = $m->selectDB($dbname);
$miles = $nearestKm['taxibase_distance_miles']; // radius
$radius = Yii::app()->generallib->generateradius($miles);
$find_users = new MongoCollection($db, "TableName"); // Access reuired table in MongoDB
$cond_user = array("latlon" => array('$near' => array(floatval($post['pick_lat']),floatval($post['pick_lon'])),'$maxDistance' => floatval($miles/111.12))));
$all_users = $find_users->find($cond_user);

(2) Create database in Mongo Db using Rock Mongo

Please refer below link to create Database in Rock Mongo.

(3) Create 2d Index in the Table as shown in the screen shot

“$near” function is available in MongoDB to get ‘Nearby’ Users based on their location. For using “$near”, we have to create “2d Index” in MongoDB. You can create “2d Index” from Rock Mongo Admin as shown in below screenshot.

Use Syntax for creating “2d Index” like below
db.places.createIndex( { “locs”: “2d” } )
Once, your index is created, you can find things as below shown screenshot

Created index will be shown as below

(4) Execute the main.php file

Once, your “2d” index is created, run the main.php file to see nearby users for a particular location.

Recently, we have added this feature in one of our applications. We are developing taxi application in which client wanted to add such feature that helps users to find a nearby taxi. We have used Mongo DB and PHP to develop on-demand taxi app like Uber and also integrate the same feature, according to the client’s requirements.