<?php
$m = new MongoClient; $c = $m->demo->pubs; $c->drop();
$c->createIndex( [ 'l' => '2d' ] );
$c->insert([ 'name' => 'Betsy Smith', 'l' => [ -0.193, 51.537 ] ]);
$c->insert([ 'name' => 'London Tavern', 'l' => [ -0.202, 51.545 ] ]);
$closest = $m->demo->command( [
'geoNear' => 'pubs',
'near' => [ -0.198, 51.538 ],
'spherical' => true,
] );
foreach ( $closest['results'] as $res ) {
printf( "%s: %.2f km\n", $res['obj']['name'], $res['dis'] * 6378 );
}
?>