The corresponding function of text search is introduced in the previous blog post.
The MongoDB database provides a series of indexing and querying mechanisms for processing operations of spatial information. This blog post will show you how to create and apply spatial indexes on the ruby driver. The following example uses a simple collection called restaurants in the test database.
Here is the restaurants collection
{"Address": {"Building": "1007", "coord": [ -73.856077,40.848447], "Street": "Morris Park Ave", "ZIPC Ode ":" 10462 "}," borough ":" Bronx "," cuisine ":" Bakery "," grades ": {{" date ": {" $date ": 1393804800000}," Grade ":" A "," Score ": 2}, {" date ": {" $date ": 1299152000000}," Grade ":" B "," Score ": +}," name ":" Morris Park Bake Shop "," restaurant_id ":" 30075445 "}
The following code creates an index of 2dsphere on the Address.coord field.
Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' test ') client[:restaurant].indexes.create_one ({' Address.coord ' = ' 2dsphere '})
Once this index has been created, it can be used above, such as $near, $geoWithin, $geoIntersects operator. The following example shows how to find all distance coordinates in the collection by the $near operator. Hotels with distances not exceeding 500 m
Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' test ') collection=client[:restaurants] Collection.find ( {' Address.coord ' => { "$near" =>{ "$geometry" =>{ {"$type" = "point", "coordinates" =>[-73.96,40.78]}, "$maxDistance" =>500 } } } }) .each do |doc| p docend
Use the $geowithin operator to find out all of the document information for all location information within a given polygon boundary range.
Client=mongo::client.new ([' mongodb://127.0.0.1:27017/test ']) collection=client[:restaurants]collection.find ( { "Address.coord" = { {"$geoWithIn" => { "$geometry" => {"Type" = "Ploygon", "Corrdinates" => [[[-73,48],[-74,41],[-72,39],[-73,40]]] } } } } } ) .each do |doc| p doc end
The space operation of MongoDB ends here
This article from "Techfuture" blog, declined reprint!
Ruby Operation MongoDB (Advanced 11)--Spatial information search geospatial