Docs Menu
Docs Home
/ / /
Java ๋™๊ธฐํ™” ๋“œ๋ผ์ด๋ฒ„
/ / /

์ง€๋ฆฌ๊ณต๊ฐ„์  ๊ฒ€์ƒ‰

์ด ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ

  • ๊ฐœ์š”
  • ์ง€๊ตฌ์ƒ์˜ ์ขŒํ‘œ
  • GeoJSON ์œ„์น˜
  • GeoJSON ์œ ํ˜•
  • GeoJSON ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์„œ ์‚ฝ์ž…
  • Index
  • 2D ํ‰๋ฉด์˜ ์ขŒํ‘œ
  • ๋ ˆ๊ฑฐ์‹œ ์ขŒํ‘œ๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์„œ ์‚ฝ์ž…
  • Index
  • ์ง€๋ฆฌ๊ณต๊ฐ„ ์ฟผ๋ฆฌ
  • ์ฟผ๋ฆฌ ์—ฐ์‚ฐ์ž
  • ์ฟผ๋ฆฌ ๋งค๊ฐœ๋ณ€์ˆ˜
  • ์˜ˆ์‹œ
  • ๊ทผ์ ‘์„ฑ ๊ธฐ์ค€ ์ฟผ๋ฆฌ
  • ๋ฒ”์œ„ ๋‚ด ์ฟผ๋ฆฌ

์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” MongoDB Java ๋“œ๋ผ์ด๋ฒ„๋กœ ์ง€๋ฆฌ ๊ณต๊ฐ„์  ๋ฐ์ดํ„ฐ ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ MongoDB์—์„œ ์ง€์›ํ•˜๋Š” ๋‹ค์–‘ํ•œ ์ง€๋ฆฌ ๊ณต๊ฐ„์  ๋ฐ์ดํ„ฐ ํ˜•์‹์— ๋Œ€ํ•ด ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ง€๋ฆฌ ๊ณต๊ฐ„์  ๋ฐ์ดํ„ฐ๋Š” ์ง€๊ตฌ ํ‘œ๋ฉด์˜ ์ง€๋ฆฌ์  ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ์ง€๋ฆฌ ๊ณต๊ฐ„์  ๋ฐ์ดํ„ฐ์˜ ์˜ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ์˜ํ™”๊ด€ ์œ„์น˜

  • ๊ตญ๊ฐ€ ๊ฒฝ๊ณ„

  • ์ž์ „๊ฑฐ ๋ผ์ด๋”ฉ ๊ฒฝ๋กœ

  • ๋‰ด์š•์˜ ๋ฐ˜๋ ค๊ฒฌ ์šด๋™ ๊ณต๊ฐ„

์ง€๋ฆฌ ๊ณต๊ฐ„์  ๋ฐ์ดํ„ฐ๋ฅผ MongoDB์— ์ €์žฅํ•˜๊ณ  ์ฟผ๋ฆฌํ•˜๋ ค๋ฉด GeoJSON ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. GeoJSON์€ IETF(์ธํ„ฐ๋„ท ์—”์ง€๋‹ˆ์–ด๋ง ํƒœ์Šคํฌํฌ์Šค)์—์„œ ๊ฐœ๋ฐœํ•œ ๋ฐ์ดํ„ฐ ํ˜•์‹์ž…๋‹ˆ๋‹ค.

GeoJSON ๋‚ด MongoDB ๋ณธ์‚ฌ์˜ ์œ„์น˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

"MongoDB Headquarters" : {
"type": "point",
"coordinates": [-73.986805, 40.7620853]
}

GeoJSON์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๊ณต์‹ IETF ์‚ฌ์–‘์„ ์ฐธ์กฐํ•˜์„ธ์š”.

์œ„์น˜๋Š” ์ง€๊ตฌ์ƒ์˜ ๋‹จ์ผ ์žฅ์†Œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ ์ฝ”๋“œ์— ๋‘์„ธ ๊ฐœ์˜ ์ˆซ์ž ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ๋ฐฐ์—ด๋กœ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

  • ์ฒซ ๋ฒˆ์งธ ์œ„์น˜์˜ ๊ฒฝ๋„(ํ•„์ˆ˜)

  • ๋‘ ๋ฒˆ์งธ ์œ„์น˜์˜ ์œ„๋„(ํ•„์ˆ˜)

  • ์„ธ ๋ฒˆ์งธ ์œ„์น˜์˜ ๊ณ ๋„(์„ ํƒ ์‚ฌํ•ญ)

์ค‘์š”

๊ฒฝ๋„, ์œ„๋„ ์ˆœ์„œ

GeoJSON์€ ์ขŒํ‘œ๋ฅผ ๊ฒฝ๋„๊ฐ€ ๋จผ์ €, ์œ„๋„๊ฐ€ ๋‘ ๋ฒˆ์งธ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ์ง€๋ฆฌ์  ์ขŒํ‘œ๊ณ„ ๊ทœ์น™์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์œ„๋„๋ฅผ ๋จผ์ € ๋‚˜์—ดํ•˜๊ณ  ๊ฒฝ๋„๋ฅผ ๋‚˜์ค‘์— ๋‚˜์—ดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜์™ธ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž‘์—… ์ค‘์ธ ๋‹ค๋ฅธ ๋„๊ตฌ๊ฐ€ ์–ด๋–ค ํ˜•์‹์„ ์‚ฌ์šฉํ•˜๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. OpenStreetMap ๋ฐ Google Maps์™€ ๊ฐ™์ด ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ๋„๊ตฌ๋Š” ์ขŒํ‘œ๋ฅผ ์œ„๋„, ๊ฒฝ๋„ ์ˆœ์œผ๋กœ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค.

GeoJSON ๊ฐ์ฒด์˜ ์œ ํ˜•์— ๋”ฐ๋ผ ๊ธฐํ•˜ํ•™์  ๋ชจ์–‘์ด ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ๊ธฐํ•˜ํ•™์  ๋„ํ˜•์€ ์œ„์น˜๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ๋ช‡ ๊ฐ€์ง€ ์ผ๋ฐ˜์ ์ธ GeoJSON ์œ ํ˜•๊ณผ ์ด๋ฅผ ์œ„์น˜๋กœ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค:

  • Point: ๋‹จ์ผ ์œ„์น˜. ์ด๋Š” ์กฐ๊ฐ ์˜ ์œ„์น˜ ๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • LineString: ๋‘ ๊ฐœ ์ด์ƒ์˜ ์œ„์น˜๋กœ ๊ตฌ์„ฑ๋œ ๋ฐฐ์—ด ๋กœ, ์ผ๋ จ์˜ ๋ผ์ธ ์„ธ๊ทธ๋จผํŠธ๋ฅผ ํ˜•์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋งŒ๋ฆฌ์žฅ์„ฑ์˜ ๊ฒฝ๋กœ๋ฅผ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Polygon: ์ฒซ ๋ฒˆ์งธ ์œ„์น˜์™€ ๋งˆ์ง€๋ง‰ ์œ„์น˜๊ฐ€ ๋™์ผํ•˜์—ฌ ์ผ๋ถ€ ๊ณต๊ฐ„์„ ๋‘˜๋Ÿฌ์‹ธ๋Š” ์œ„์น˜ ๋ฐฐ์—ด ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋ฐ”ํ‹ฐ์นธ ์‹œ๊ตญ ๋‚ด์˜ ๋•…์„๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

MongoDB์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ์–‘์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด GeoJSON ์ˆ˜๋™ ํ•ญ๋ชฉ์„ ์ฐธ์กฐํ•˜์„ธ์š”.

GeoJSON ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฌธ์„œ ๋ฅผ ์‚ฝ์ž…ํ•˜๋ ค๋ฉด GeoJSON ๊ฐ’์ด ํฌํ•จ๋œ ๋ฌธ์„œ ๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฌธ์„œ ๋ฅผ insertOne() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์‹œ ์—์„œ๋Š” GeoJSON ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” location.geo ํ•„๋“œ ๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์„œ ๋ฅผ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.

// Add your MongoCollection setup code here
Point point = new Point(new Position(-74.0065, 40.7085));
Document theater = new Document("theaterId", 1203)
.append("location", new Document("geo", point));
InsertOneResult result = collection.insertOne(theater);

๋ฌธ์„œ ์‚ฝ์ž…์— ํ•™์Šต ๋ณด๋ ค๋ฉด ์‚ฝ์ž… ์ž‘์—… ๊ฐ€์ด๋“œ ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

GeoJSON ํ˜•์‹์œผ๋กœ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฟผ๋ฆฌํ•˜๋ ค๋ฉด GeoJSON ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋œ ํ•„๋“œ๋ฅผ 2dsphere ์ธ๋ฑ์Šค์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์Šค๋‹ˆํŽซ์€ Indexes ๋นŒ๋”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ location.geo ํ•„๋“œ์— 2dsphere ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

// <MongoCollection setup code here>
collection.createIndex(Indexes.geo2dsphere("location.geo"));

Indexes ๋นŒ๋”์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ธ๋ฑ์Šค ๋นŒ๋” ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

2์ฐจ์› ์œ ํด๋ฆฌ๋“œ ํ‰๋ฉด์— x ๋ฐ y ์ขŒํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง€๋ฆฌ ๊ณต๊ฐ„์  ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2์ฐจ์› ํ‰๋ฉด์˜ ์ขŒํ‘œ๋ฅผ "๋ ˆ๊ฑฐ์‹œ ์ขŒํ‘œ ์Œ"์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

legacy coordinate pairs์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

"<field name>" : [ x, y ]

ํ•„๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด x ์ถ• ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๊ณ  ๋‘ ๋ฒˆ์งธ ๊ฐ’์ด y ์ถ• ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ๊ฐ’์˜ ๋ฐฐ์—ด์„ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋ ˆ๊ฑฐ์‹œ ์ขŒํ‘œ ์Œ์„ ์ €์žฅํ•˜๋Š” ๋ฌธ์„œ ๋ฅผ ์‚ฝ์ž…ํ•˜๋ ค๋ฉด ํ•„๋“œ ์— ์ขŒํ‘œ ์Œ ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ๋ฌธ์„œ ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ฌธ์„œ ๋ฅผ insertOne() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์‹œ ์—์„œ๋Š” ๋ ˆ๊ฑฐ์‹œ ์ขŒํ‘œ ์Œ์ด ํฌํ•จ๋œ coordinates ํ•„๋“œ ๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์„œ ๋ฅผ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.

// Add your MongoCollection setup code here
Document theater = new Document("theaterId", 1204)
.append("coordinates", Arrays.asList(-73.9862, 40.7311));
InsertOneResult result = collection.insertOne(theater);

๋ฌธ์„œ ์‚ฝ์ž…์— ํ•™์Šต ๋ณด๋ ค๋ฉด ์‚ฝ์ž… ์ž‘์—… ๊ฐ€์ด๋“œ ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

๋ ˆ๊ฑฐ์‹œ ์ขŒํ‘œ ์Œ์œผ๋กœ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฟผ๋ฆฌํ•˜๋ ค๋ฉด ๋ ˆ๊ฑฐ์‹œ ์ขŒํ‘œ ์Œ์ด ํฌํ•จ๋œ ํ•„๋“œ๋ฅผ 2d ์ธ๋ฑ์Šค์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์Šค๋‹ˆํŽซ์€ Indexes ๋นŒ๋”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ coordinates ํ•„๋“œ์— 2d ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

// <MongoCollection setup code here>
collection.createIndex(Indexes.geo2d("coordinates"));

Indexes ๋นŒ๋”์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ธ๋ฑ์Šค ๋นŒ๋” ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

MongoDB Server legacy coordinate pairs๋ ˆ๊ฑฐ์‹œ ์ขŒํ‘œ์Œ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์— legacy coordinate pairs ๋Œ€ํ•œ ๋งค๋‰ด์–ผ ํŽ˜์ด์ง€๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

ํŒ

์ง€์›๋˜๋Š” ์—ฐ์‚ฐ์ž

๊ตฌํ˜•(2dsphere) ๋ฐ ํ‰๋ฉด(2d) ์ธ๋ฑ์Šค๋Š” ์ „๋ถ€๋Š” ์•„๋‹ˆ์ง€๋งŒ ์ผ๋ถ€ ๋™์ผํ•œ ์ฟผ๋ฆฌ ์—ฐ์‚ฐ์ž๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ์—ฐ์‚ฐ์ž ๋ชฉ๋ก๊ณผ ์ธ๋ฑ์Šค ํ˜ธํ™˜์„ฑ์€ ์ง€๋ฆฌ ๊ณต๊ฐ„์  ์ฟผ๋ฆฌ ์ˆ˜๋™ ํ•ญ๋ชฉ์„ ์ฐธ์กฐํ•˜์„ธ์š”.

์ง€๋ฆฌ ๊ณต๊ฐ„์  ์ฟผ๋ฆฌ๋Š” ์ฟผ๋ฆฌ ์—ฐ์‚ฐ์ž์™€ ์ฟผ๋ฆฌ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ GeoJSON ๋„ํ˜•์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

์ง€๋ฆฌ ๊ณต๊ฐ„์  ๋ฐ์ดํ„ฐ๋ฅผ ์ฟผ๋ฆฌํ•˜๋ ค๋ฉด ๋‹ค์Œ ์ฟผ๋ฆฌ ์—ฐ์‚ฐ์ž ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค:

  • $near

  • $geoWithin

  • $nearSphere

  • $geoIntersects 2dsphere ์ธ๋ฑ์Šค ํ•„์š”

Filters ๋นŒ๋” ํด๋ž˜์Šค์˜ near(), geoWithin(), nearSphere() ๋ฐ geoIntersects() ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ MongoDB Java ๋“œ๋ผ์ด๋ฒ„์—์„œ ์ด๋Ÿฌํ•œ ์ฟผ๋ฆฌ ์—ฐ์‚ฐ์ž๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ง€๋ฆฌ ๊ณต๊ฐ„์  ์ฟผ๋ฆฌ ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ง€๋ฆฌ ๊ณต๊ฐ„์  ์ฟผ๋ฆฌ ์ˆ˜๋™ ํ•ญ๋ชฉ ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

Filters ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ํ•„ํ„ฐ ๋นŒ๋” ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

์ง€๋ฆฌ ๊ณต๊ฐ„์  ์ฟผ๋ฆฌ์—์„œ ์‚ฌ์šฉํ•  ํ˜•ํƒœ๋ฅผ ์ง€์ •ํ•˜๋ ค๋ฉด MongoDB Java ๋“œ๋ผ์ด๋ฒ„์˜ Position, Point, LineString ๋ฐ Polygon ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

MongoDB Java ๋“œ๋ผ์ด๋ฒ„์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” GeoJSON ๋ชจ์–‘์˜ ์ „์ฒด ๋ชฉ๋ก์€ GeoJSON ํŒจํ‚ค์ง€ ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. API ๋ฌธ์„œ.

๋‹ค์Œ ์˜ˆ์‹œ์—์„œ๋Š” MongoDB Atlas ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋น ๋ฅธ ์‹œ์ž‘ ๊ฐ€์ด๋“œ์—์„œ ํ”„๋ฆฌ ํ‹ฐ์–ด Atlas ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ์˜ˆ์—์„œ๋Š” ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ์˜ sample_mflix ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์žˆ๋Š” theaters ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 2} ์ปฌ๋ ‰์…˜์— ํ•„๋“œ์— theaters 2dsphere ์ธ๋ฑ์Šค๊ฐ€ ํฌํ•จ๋˜์–ด location.geo ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ€์ ธ์˜ค๊ธฐ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

import java.util.Arrays;
import org.bson.conversions.Bson;
import com.mongodb.client.model.geojson.Point;
import com.mongodb.client.model.geojson.Polygon;
import com.mongodb.client.model.geojson.Position;
import static com.mongodb.client.model.Filters.near;
import static com.mongodb.client.model.Filters.geoWithin;
import static com.mongodb.client.model.Projections.fields;
import static com.mongodb.client.model.Projections.include;
import static com.mongodb.client.model.Projections.excludeId;

์—ฌ๊ธฐ์—์„œ Github์—์„œ ์˜ˆ์ œ์˜ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ ์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ฒƒ๋ถ€ํ„ฐ ๊ฐ€์žฅ ๋จผ ๊ฒƒ ์ˆœ์œผ๋กœ ๋ฌธ์„œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๋ ค๋ฉด Filters ๋นŒ๋” ํด๋ž˜์Šค์˜ near() ์ •์  ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. near() ๋ฉ”์„œ๋“œ๋Š” $near ์ฟผ๋ฆฌ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์‹œ 10,000 ๋Š” 5,000 ์„ผํŠธ๋Ÿด ๊ณต์›์˜ ๊ทธ๋ ˆ์ดํŠธ ๋ก  ์—์„œ ~ ๋ฏธํ„ฐ ๊ฑฐ๋ฆฌ์—์žˆ๋Š” ๊ทน์žฅ์„ ์ฟผ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

// Add your MongoClient setup code here
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection<Document> collection = database.getCollection("theaters");
Point centralPark = new Point(new Position(-73.9667, 40.78));
// Creates a query that matches all locations between 5,000 and 10,000 meters from the specified Point
Bson query = near("location.geo", centralPark, 10000.0, 5000.0);
// Creates a projection to include only the "location.address.city" field in the results
Bson projection = fields(include("location.address.city"), excludeId());
// Prints the projected field of the results from the geospatial query as JSON
collection.find(query)
.projection(projection)
.forEach(doc -> System.out.println(doc.toJson()));

์•ž์˜ ์ฝ”๋“œ์˜ ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

{"location": {"address": {"city": "Bronx"}}}
{"location": {"address": {"city": "New York"}}}
{"location": {"address": {"city": "New York"}}}
{"location": {"address": {"city": "Long Island City"}}}
{"location": {"address": {"city": "New York"}}}
{"location": {"address": {"city": "Secaucus"}}}
{"location": {"address": {"city": "Jersey City"}}}
{"location": {"address": {"city": "Elmhurst"}}}
{"location": {"address": {"city": "Flushing"}}}
{"location": {"address": {"city": "Flushing"}}}
{"location": {"address": {"city": "Flushing"}}}
{"location": {"address": {"city": "Elmhurst"}}}

ํŒ

ํฅ๋ฏธ๋กœ์šด ์‚ฌ์‹ค

MongoDB๋Š” GPS ์ธ๊ณต์œ„์„ฑ ๊ณผ ๋™์ผํ•œ ์ฐธ์กฐ ์‹œ์Šคํ…œ ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ง€๊ตฌ์˜ ๊ธฐํ•˜ํ•™์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

$near ์—ฐ์‚ฐ์ž ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ $near์— ๋Œ€ํ•œ ์ฐธ๊ณ  ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

Filters ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ํ•„ํ„ฐ ๋นŒ๋” ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

์ง€์ •๋œ ๋ชจ์–‘ ๋‚ด์—์„œ ์ง€๋ฆฌ ๊ณต๊ฐ„์  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋ ค๋ฉด Filters ๋นŒ๋” ํด๋ž˜์Šค์˜ geoWithin() ์ •์  ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. geoWithin() ๋ฉ”์„œ๋“œ๋Š” $geoWithin ์ฟผ๋ฆฌ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์—์„œ๋Š” ๋กฑ ์•„์ผ๋žœ๋“œ์˜ ํ•œ ๊ตฌ์—ญ์— ์žˆ๋Š” ์˜ํ™”๊ด€์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

// Add your MongoCollection setup code here
// Creates a set of points that defines the bounds of a geospatial shape
Polygon longIslandTriangle = new Polygon(Arrays.asList(new Position(-72, 40),
new Position(-74, 41),
new Position(-72, 39),
new Position(-72, 40)));
// Creates a projection to include only the "location.address.city" field in the results
Bson projection = fields(include("location.address.city"), excludeId());
// Creates a query that matches documents containing "location.geo" values within the specified bounds
Bson geoWithinComparison = geoWithin("location.geo", longIslandTriangle);
// Prints the projected field of the results from the geolocation query as JSON
collection.find(geoWithinComparison)
.projection(projection)
.forEach(doc -> System.out.println(doc.toJson()));

์•ž์˜ ์ฝ”๋“œ์˜ ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

{"location": {"address": {"city": "Baldwin"}}}
{"location": {"address": {"city": "Levittown"}}}
{"location": {"address": {"city": "Westbury"}}}
{"location": {"address": {"city": "Mount Vernon"}}}
{"location": {"address": {"city": "Massapequa"}}}

๋‹ค์Œ ๊ทธ๋ฆผ์€ longIslandTriangle ๋ณ€์ˆ˜๋กœ ์ •์˜๋œ ๋‹ค๊ฐํ˜•๊ณผ ์ฟผ๋ฆฌ์—์„œ ๋ฐ˜ํ™˜๋œ ์˜ํ™”๊ด€ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

์˜ํ™”๊ด€์—์„œ ๊ฒ€์ƒ‰ ์ค‘์ธ ๋กฑ ์•„์ผ๋žœ๋“œ ์ง€์—ญ

$geoWithin ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ $geoWithin์— ๋Œ€ํ•œ ์ฐธ๊ณ  ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š” .

์ฟผ๋ฆฌ ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ง€๋ฆฌ ๊ณต๊ฐ„์  ์ฟผ๋ฆฌ ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•œ MongoDB Server ๋งค๋‰ด์–ผ ํŽ˜์ด์ง€๋ฅผ์ฐธ์กฐํ•˜์„ธ์š”.

๋Œ์•„๊ฐ€๊ธฐ

๋ฐ˜ํ™˜ํ•  ํ•„๋“œ ์ง€์ •