Ruby Operation MongoDB (Advanced 11)--Spatial information search geospatial

Source: Internet
Author: User
Tags mongodb

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" =&GT [[[-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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.