我的文档的坐标属性上有一个二维索引。使用 mongo shell,我可以像这样查询集合;
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 会在同一查询中返回零结果?
更新 #1 看起来 Doctrine\MongoDB\Query\Builder::near() L363 存在问题。方法参数忽略第二个值 ($y)。所以只有第一个值被传递来执行。