我正在处理处理地理位置信息的MongoDB集合。所以,
我创建了一个下面的集合索引。
raise ValidationError("This username has been registered!" +str( serializer.data))
我插入并通过PHP找到了一条记录(使用db.places.createIndex( { location: "2dsphere" } )
),如下所示
phalcon
我收到以下错误消息:
$lng = (float) -73.9667;
$lat = (float) 40.78;
$m = new Store();
$m->location = [
"type" => 'Point',
"coordinates" => [$lng, $lat]
] ;
print_r($m->location);
$m->save();
...
$params = [
[
"location" => [
'$near' => [
'$geometry' => [
"type" => "Point",
"coordinates" => [ $lng, $lat ]
],
'$minDistance' => 1,
'$maxDistance' => 5000
]
]
]
];
$find = Store::find($params);
print_r(count($find));
我认为2dshere格式不正确,是吗?
答案 0 :(得分:0)
错误消息表示您需要传递位置对象,而是传递位置数组。
2dsphere上的MongoDB文档使用JSON对象
https://docs.mongodb.com/manual/core/2dsphere/
db.places.insert( { loc : { type: "Point", coordinates: [ -73.97, 40.77 ] }, name: "Central Park", category : "Parks" } )
所以我猜解决方案是将数组转换为对象,类似于:
$m->location = (object) [
"type" => 'Point',
"coordinates" => [$lng, $lat]
];