You are browsing a version that has not yet been released.

Geospatial Queries

You can execute some special queries when using geospatial indexes like checking for documents within a rectangle or circle.

Mapping

First, setup some documents like the following:

  • PHP
    1<?php /** * @Document * @Index(keys={"coordinates"="2d"}) */ class City { /** @Id */ public $id; /** @Field(type="string") */ public $name; /** @EmbedOne(targetDocument=Coordinates::class) */ public $coordinates; } /** @EmbeddedDocument */ class Coordinates { /** @Field(type="float") */ public $x; /** @Field(type="float") */ public $y; }
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
  • XML
    1<indexes> <index> <key name="coordinates" order="2d" /> </index> </indexes>
    2
    3
    4
    5

Near Query

Now you can execute queries against these documents like the following. Check for the 10 nearest cities to a given longitude and latitude with the near($longitude, $latitude) method:

1<?php $cities = $this->dm->createQuery(City::class) ->field('coordinates')->near(-120, 40) ->execute();
2
3
4
5