$nearSphere
演算子では、ポイント (パラメーターを指定して定義できる「球」の中心) のみを指定できますmaxDistance
。したがって、唯一の意味のある答えは、形状が最も近いものから最も遠いものへとどのようにソートされるかについてです。
答えは、等距離の「最近接」ポイントを持つすべての形状が、さらに最近接ポイントを持つ形状より先にソートされるということです。つまり、点と形状の間の距離は、点と形状上の最も近い点の間の距離です。
$nearSphere
which isコマンドのバリアントを実行すると、$geoNear
計算された距離が返され、それを調べることができます。
サンプルデータ:
> db.near.find()
{ "_id" : 10, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 5 ], [ 5, 5 ], [ 5, 0 ], [ 0, 0 ] ] ] } }
{ "_id" : 11, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 1, 1 ], [ 1, 5 ], [ 5, 5 ], [ 5, 1 ], [ 1, 1 ] ] ] } }
{ "_id" : 12, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [ 0, 0 ] ] ] } }
{ "_id" : 13, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [0, 0 ] ], [ [ 1, 1 ], [ 1, 5 ], [ 5, 5 ], [ 5, 1 ], [ 1, 1 ] ] ] } }
{ "_id" : 14, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [ 0, 0 ] ], [ [ 0.1, 0.1 ], [ 0.1, 5 ], [ 5, 5 ], [ 5, 0.1 ], [ 0.1, 0.1 ] ] ] } }
{ "_id" : 15, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 1 ], [ 1, 1 ], [ 1, 0 ], [0, 0 ] ], [ [ 0.1, 0.1 ], [ 0.1, 0.5 ], [ 0.5, 0.5 ], [ 0.5, 0.1 ], [ 0.1, 0.1 ] ] ] } }
{ "_id" : 16, "loc" : { "type" : "LineString", "coordinates" : [ [ 0, 0 ], [ 0.1, 0.1 ] ] } }
{ "_id" : 17, "loc" : { "type" : "LineString", "coordinates" : [ [ 0, 0 ], [ 0, 0.1 ] ] } }
{ "_id" : 18, "loc" : { "type" : "LineString", "coordinates" : [ [ 0, 0 ], [ 0.1, 0 ] ] } }
$nearSphere での結果:
{ "_id" : 16, "loc" : { "type" : "LineString", "coordinates" : [ [ 0, 0 ], [ 0.1, 0.1 ] ] } }
{ "_id" : 18, "loc" : { "type" : "LineString", "coordinates" : [ [ 0, 0 ], [ 0.1, 0 ] ] } }
{ "_id" : 14, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [ 0, 0 ] ], [ [ 0.1, 0.1 ], [ 0.1, 5 ], [ 5, 5 ], [ 5, 0.1 ], [ 0.1, 0.1 ] ] ] } }
{ "_id" : 13, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [ 0, 0 ] ], [ [ 1, 1 ], [ 1, 5 ], [ 5, 5 ], [ 5, 1 ], [ 1, 1 ] ] ] } }
{ "_id" : 15, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 1 ], [ 1, 1 ], [ 1, 0 ], [ 0, 0 ] ], [ [ 0.1, 0.1 ], [ 0.1, 0.5 ], [ 0.5, 0.5 ], [ 0.5, 0.1 ], [ 0.1, 0.1 ] ] ] } }
{ "_id" : 12, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [ 0, 0 ] ] ] } }
{ "_id" : 17, "loc" : { "type" : "LineString", "coordinates" : [ [ 0, 0 ], [ 0, 0.1 ] ] } }
{ "_id" : 10, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 5 ], [ 5, 5 ], [ 5, 0 ], [ 0, 0 ] ] ] } }
{ "_id" : 11, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 1, 1 ], [ 1, 5 ], [ 5, 5 ], [ 5, 1 ], [ 1, 1 ] ] ] } }
$geoSphereコマンドを実行すると、ポイントからシェイプまでの距離を確認できます (スペースを考慮して座標は省略しています)。
> db.runCommand({geoNear:"near", near: {type:"Point", coordinates:[0,0]}, spherical:true})
[
{
"dis" : 0,
"obj" : {
"_id" : 16,
"loc" : {
"type" : "LineString"
}
}
},
{
"dis" : 0,
"obj" : {
"_id" : 18,
"loc" : {
"type" : "LineString"
}
}
},
{
"dis" : 0,
"obj" : {
"_id" : 14,
"loc" : {
"type" : "Polygon"
}
}
},
{
"dis" : 0,
"obj" : {
"_id" : 13,
"loc" : {
"type" : "Polygon"
}
}
},
{
"dis" : 0,
"obj" : {
"_id" : 15,
"loc" : {
"type" : "Polygon"
}
}
},
{
"dis" : 0,
"obj" : {
"_id" : 12,
"loc" : {
"type" : "Polygon"
}
}
},
{
"dis" : 0,
"obj" : {
"_id" : 17,
"loc" : {
"type" : "LineString"
}
}
},
{
"dis" : 0,
"obj" : {
"_id" : 10,
"loc" : {
"type" : "Polygon"
}
}
},
{
"dis" : 157424.6238723255,
"obj" : {
"_id" : 11,
"loc" : {
"type" : "Polygon"
}
}
}
]
ご覧のとおり、すべての形状 ([1,1] で開始した最後の形状を除く) の点 [0,0] までの距離は、形状のサイズと形状がすべて異なっていても 0 でした。指定した点までの距離が同じドキュメント内では、順序は定義されていません。つまり、決定論的に見えても、同じであるとは限りません。