Skip to content

Instantly share code, notes, and snippets.

@tqyq
Last active April 7, 2016 08:15
Show Gist options
  • Save tqyq/e2a990915f1288068727 to your computer and use it in GitHub Desktop.
Save tqyq/e2a990915f1288068727 to your computer and use it in GitHub Desktop.
php mongodb geo index query by loc
// run below in mongo client shell
db.teacher.createIndex( { "loc": "2d" } )
// php query
public function test($dis=1) {
$r = D('Teacher')->command([
'geoNear'=>'teacher',
'near'=>[116.32723, 39.977331],
'maxDistance' => $dis/111,
'distanceMultiplier' => 111,
'num' => 2000,
'query'=>['audit'=>'1'],
]);
json_out($r);
}
// another example
$mongo = new Mongo();
$result = $mongo->selectDB('users')->command(array(
'geoNear' => 'user',
'near' => $lnglat,
'spherical' => true,
'num' => 10 ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment