$geoWithin
์ ์
$geoWithin
์ง์ ๋ ๋ชจ์ ์์๋ง ์กด์ฌํ๋ ์ง๋ฆฌ ๊ณต๊ฐ์ ๋ฐ์ดํฐ๊ฐ ์๋ ๋ฌธ์๋ฅผ ์ ํํฉ๋๋ค.
์ง์ ๋ ๋ชจ์์ GeoJSON
Polygon
(๋จ์ผ ๊ณ ๋ฆฌ ๋๋ ๋ค์ค ๊ณ ๋ฆฌ), GeoJSONMultiPolygon
๋๋ ๊ธฐ์กด ์ขํ ์์ผ๋ก ์ ์๋ ๋ชจ์์ผ ์ ์์ต๋๋ค.$geoWithin
์ฐ์ฐ์๋$geometry
์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ GeoJSON ๊ฐ์ฒด๋ฅผ ์ง์ ํฉ๋๋ค.๊ธฐ๋ณธ ์ขํ๊ณ(CRS)๋ฅผ ์ฌ์ฉํ์ฌ GeoJSON ๋ค๊ฐํ ๋๋ ๋ค์ค ๋ค๊ฐํ์ ์ง์ ํ๋ ค๋ฉด ๋ค์ ๊ตฌ๋ฌธ์ ์ฌ์ฉํฉ๋๋ค.
{ <location field>: { $geoWithin: { $geometry: { type: <"Polygon" or "MultiPolygon"> , coordinates: [ <coordinates> ] } } } } ๋จ์ผ ๋ฐ๊ตฌ๋ณด๋ค ๋ฉด์ ์ด ํฐ GeoJSON ๋ํ์ ์ง์ ํ๋
$geoWithin
์ฟผ๋ฆฌ์ ๊ฒฝ์ฐ, ๊ธฐ๋ณธ CRS๋ฅผ ์ฌ์ฉํ๋ฉด ๋ณด์์ ์ธ ๋ํ์ ๋ํ ์ฟผ๋ฆฌ๊ฐ ์์ฑ๋ฉ๋๋ค.์ฌ์ฉ์ ์ง์ MongoDB CRS๋ฅผ ์ฌ์ฉํ์ฌ ๋จ์ผ ๊ณ ๋ฆฌ GeoJSON ๋ค๊ฐํ์ ์ง์ ํ๋ ค๋ฉด
$geometry
ํํ์์ ์ฌ์ฉ์ ์ง์ MongoDB CRS๋ฅผ ์ง์ ํ๋ ๋ค์ ํ๋กํ ํ์ ์ ์ฌ์ฉํ์ธ์.{ <location field>: { $geoWithin: { $geometry: { type: "Polygon" , coordinates: [ <coordinates> ], crs: { type: "name", properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" } } } } } } ์ฌ์ฉ์ ์ง์ MongoDB CRS๋ ์๊ณ ๋ฐ๋ ๋ฐฉํฅ์ ์์ธ๋ฉ ์์๋ฅผ ์ฌ์ฉํ๋ฉฐ,
$geoWithin
๊ฐ ๋จ์ผ ๋ฐ๊ตฌ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ๋ฉด์ ์ ๊ฐ๋ ๋จ์ผ ๊ณ ๋ฆฌ GeoJSON ๋ค๊ฐํ์ด ์๋ ์ฟผ๋ฆฌ๋ฅผ ์ง์ํ ์ ์๋๋ก ํฉ๋๋ค. ์ง์ ๋ ๋ค๊ฐํ์ด ๋จ์ผ ๋ฐ๊ตฌ๋ณด๋ค ์์ ๊ฒฝ์ฐ MongoDB CRS๋ฅผ ์ฌ์ฉํ$geoWithin
์ ๋์์ ๊ธฐ๋ณธ CRS๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ์ ๋์ผํฉ๋๋ค. 'ํฐ' ๋ค๊ฐํ๋ ์ฐธ์กฐํ์ธ์.ํ๋ฉด์ ๋ ๊ฑฐ์ ์ขํ ์์ผ๋ก ์ ์๋ ๋ชจ์์ ํฌํจ๋๋๋ก ์ฟผ๋ฆฌํ๋ ๊ฒฝ์ฐ ๋ค์ ๊ตฌ๋ฌธ์ ์ฌ์ฉํฉ๋๋ค.
{ <location field>: { $geoWithin: { <shape operator>: <coordinates> } } } ์ฌ์ฉ ๊ฐ๋ฅํ ๋ชจ์ ์ฐ์ฐ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
$center
(์์ ์ ์ํฉ๋๋ค), ๊ทธ๋ฆฌ๊ณ$centerSphere
(๊ตฌ์ ์์ ์ ์ํฉ๋๋ค).
์ค์
๊ฒฝ๋์ ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ
longitude, latitude
์์ผ๋ก ์ขํ๋ฅผ ์ง์ ํฉ๋๋ค.
ํ๋
์ง๋ฆฌ ๊ณต๊ฐ์ ์ธ๋ฑ์ค
$geoWithin
์๋ ์ง๋ฆฌ ๊ณต๊ฐ์ ์ธ๋ฑ์ค๊ฐ ํ์ํ์ง ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ง๋ฆฌ ๊ณต๊ฐ์ ์ธ๋ฑ์ค๋ ์ฟผ๋ฆฌ ์ฑ๋ฅ์ ํฅ์ํฉ๋๋ค. 2dsphere์ 2d ์ง๋ฆฌ ๊ณต๊ฐ์ ์ธ๋ฑ์ค ๋ชจ๋ $geoWithin
๋ฅผ ์ง์ํฉ๋๋ค.
์ ๋ ฌ๋์ง ์์ ๊ฒฐ๊ณผ
$geoWithin
์ฐ์ฐ์๋ ์ ๋ ฌ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ์ง ์์ต๋๋ค. ๋ฐ๋ผ์ MongoDB๋ ๊ฒฐ๊ณผ๋ฅผ ์ ๋ ฌํ๋ ์ง๋ฆฌ ๊ณต๊ฐ์ $near
๋๋ $nearSphere
์ฟผ๋ฆฌ๋ณด๋ค $geoWithin
์ฟผ๋ฆฌ๋ฅผ ๋ ๋น ๋ฅด๊ฒ ๋ฐํํ ์ ์์ต๋๋ค.
์ง์ค๋ฉํธ๋ฆฌ ํดํ
$geoWithin
๋ค๊ฐํ์ ํฌํจํ๋ค๊ณ ๋ณด์ฅํ์ง ์์ต๋๋ค. ๊ทธ ๊ตฌ์ฑ ์์ ์ง์ค๋ฉํธ๋ฆฌ๋ฅผ ํฌํจํ๊ฑฐ๋ ๊ตฌ์ฑ ์์ ์ง์ค๋ฉํธ๋ฆฌ๋ฅผ ๊ณต์ ํ๋ ๋ค๋ฅธ ํด๋ฆฌ๊ณค์ ํฌํจํ๋ค๊ณ ๋ณด์ฅํ์ง ์์ต๋๋ค.
'ํฐ' ๋ค๊ฐํ
$geoWithin
์ ๊ฒฝ์ฐ ๋จ์ผ ๋ฐ๊ตฌ๋ณด๋ค ํฐ ๋ฉด์ ์ ๊ฐ์ง ๋จ์ผ ๊ณ ๋ฆฌ ๋ค๊ฐํ์ ์ง์ ํ๋ ๊ฒฝ์ฐ the
custom MongoDB coordinate reference system in the $geometry
ํํ์์ ํฌํจํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด $geoWithin
์ด ๋ณด์ ๊ธฐํ ๋ํ์ ์ฟผ๋ฆฌํฉ๋๋ค. ๋ฐ๊ตฌ๋ณด๋ค ํฐ ๋ฉด์ ์ ๊ฐ์ง ๋ค๋ฅธ ๋ชจ๋ GeoJSON ๋ค๊ฐํ์ ๊ฒฝ์ฐ $geoWithin
๋ ๋ณด์ ๊ธฐํ ๋ํ์ ์ฟผ๋ฆฌํฉ๋๋ค.
์์
๋ค๊ฐํ ๋ด
๋ค์ ์์์์๋ GeoJSONPolygon
๋ด์ ์์ ํ ์กด์ฌํ๋ loc
๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ ์ ํํฉ๋๋ค. ๋ค๊ฐํ์ ๋ฉด์ ์ด ๋จ์ผ ๋ฐ๊ตฌ์ ๋ฉด์ ๋ณด๋ค ์์ต๋๋ค.
db.places.find( { loc: { $geoWithin: { $geometry: { type : "Polygon" , coordinates: [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ] ] } } } } )
๋จ์ผ ๋ฐ๊ตฌ๋ณด๋ค ํฐ ๋ฉด์ ์ ๊ฐ์ง ๋จ์ผ ๊ณ ๋ฆฌ ๋ค๊ฐํ์ ๊ฒฝ์ฐ "ํฐ" ๋ค๊ฐํ ๋ด๋ถ๋ฅผ ์ฐธ์กฐํ์ธ์.
"ํฐ" ๋ค๊ฐํ ๋ด
๋ฉด์ ์ด ๋จ์ผ ๋ฐ๊ตฌ๋ณด๋ค ํฐ ๋จ์ผ ๋ง GeoJSON ๋ค๊ฐํ์ ์ฌ์ฉํ์ฌ ์ฟผ๋ฆฌํ๋ ค๋ฉด $geometry
ํํ์์ ์ฌ์ฉ์ ์ง์ MongoDB ์ขํ ์ฐธ์กฐ ์์คํ
์ ์ง์ ํด์ผ ํฉ๋๋ค. ์์:
db.places.find( { loc: { $geoWithin: { $geometry: { type : "Polygon" , coordinates: [ [ [ -100, 60 ], [ -100, 0 ], [ -100, -60 ], [ 100, -60 ], [ 100, 60 ], [ -100, 60 ] ] ], crs: { type: "name", properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" } } } } } } )