문서 메뉴
문서 홈
/ / /
Ruby MongoDB Driver
/

지리 공간적 Atlas Search

MongoDB는 지리 공간적 정보를 처리하기 위해 다양한 인덱스와 쿼리 메커니즘을 제공합니다. 이 섹션에서는 Ruby 드라이버로 지리 공간적 인덱스 를 생성하고 사용하는 방법을 설명합니다.

이 페이지의 예제에서는 test 데이터베이스의 restaurants 이라는 샘플 컬렉션을 사용합니다. 샘플 데이터 세트 를 다운로드할 수 있습니다.

다음은 restaurants collection의 문서 샘플입니다.

{
"address": {
"building": "1007",
"coord": [ -73.856077, 40.848447 ],
"street": "Morris Park Ave",
"zipcode": "10462"
},
"borough": "Bronx",
"cuisine": "Bakery",
"grades": [
{ "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
{ "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
],
"name": "Morris Park Bake Shop",
"restaurant_id": "30075445"
}

다음 예제에서는 address.coord 필드에 2dsphere 인덱스를 생성합니다.

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test' )
client[:restaurants].indexes.create_one( { 'address.coord' => '2dsphere' })

인덱스가 생성되면 $near, $geoWithin$geoIntersects 연산자 등 여러 연산자를 사용하여 인덱스를 쿼리할 수 있습니다. 다음 예제에서는 $near 연산자를 사용하여 지정된 좌표에서 500 미터 이내에 있는 모든 레스토랑을 찾습니다.

client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:restaurants]
collection.find(
{ 'address.coord' =>
{ "$near" =>
{ "$geometry" =>
{ "type" => "Point", "coordinates" => [ -73.96, 40.78 ] },
"$maxDistance" => 500
}
}
}
).each do |doc|
#=> Yields a BSON::Document.
end

주어진 다각형의 경계 내에 있는 위치가 있는 모든 문서를 찾으려면 $geoWithin 연산자를 사용합니다.

client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:restaurants]
collection.find(
{ "address.coord" =>
{ "$geoWithin" =>
{ "$geometry" =>
{ "type" => "Polygon" ,
"coordinates" => [ [ [ -73, 40 ], [ -74, 41 ], [ -72, 39 ], [ -73, 40 ] ] ]
}
}
}
}
).each do |doc|
#=> Yields a BSON::Document.
end

돌아가기

텍스트 검색

다음

쿼리 캐시