ドキュメントの座標プロパティに 2 次元インデックスがあります。mongo シェルを使用すると、次のようにコレクションを照会できます。
db.adverts.find({coordinates:{$near:[20,40]}})
そして、予想どおり、次の結果が返されます。
{ "_id" : ObjectId("4fddac51352de93903000000"), "title" : "dummy #3", "coordinates" : { "longitude" : 22, "latitude" : 31 } }
{ "_id" : ObjectId("4fddac48352de95105000000"), "title" : "dummy #3", "coordinates" : { "longitude" : 20, "latitude" : 30 } }
{ "_id" : ObjectId("4fddaca4352de93703000000"), "title" : "dummy #3", "coordinates" : { "longitude" : 31, "latitude" : 22 } }
{ "_id" : ObjectId("4fdda6a2352de90a03000000"), "title" : "dummy title", "created" : ISODate("2012-06-17T09:42:58Z"), "coordinates" : { "longitude" : 54.1234, "latitude" : -1.234 } }
{ "_id" : ObjectId("4fdda6d8352de9c004000000"), "title" : "dummy title #2", "created" : ISODate("2012-06-17T09:43:52Z"), "coordinates" : { "longitude" : 54.34, "latitude" : -1.124 } }
ただし、ドキュメントに従ってDoctrineを使用してまったく同じコレクションをクエリすると、結果が得られません。
$adverts = $dm->createQueryBuilder('Advert')
->field('coordinates')->near(20, 40)
->getQuery()
->execute();
$adverts->count(); // => 0
私の広告 yaml は次のようになります。
Advert:
type: document
collection: adverts
fields:
id:
id: true
title:
type: string
content:
type: string
created:
type: date
updated:
type: date
status:
type: int
distance:
type: int
indexes:
coordinates:
keys:
coordinates: 2d
referenceOne:
owner:
targetDocument: User
embedOne:
coordinates:
targetDocument: Coordinates
座標ドキュメントは次のようになります。
Coordinates:
type: embeddedDocument
fields:
longitude:
type: float
latitude:
type: float
Doctrine の ODM を使用すると、同じクエリでゼロの結果が返される理由はありますか?
UPDATE #1 Doctrine\MongoDB\Query\Builder::near() L363 に問題があるようです。method パラメーターは 2 番目の値 ($y) を無視します。したがって、最初の値のみが渡されて実行されます。