Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6659463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:02:14+00:00 2026-05-26T02:02:14+00:00

MongoDB has a very nice Geospatial Indexing feature. How can I use it in

  • 0

MongoDB has a very nice Geospatial Indexing feature. How can I use it in Rails with Mongoid?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:02:15+00:00Added an answer on May 26, 2026 at 2:02 am

    You can define geo indexes like this in mongoid

    class Item
      include Mongoid::Document
    
      field :loc, :type => Array
    
      index(
          [
              [:loc, Mongo::GEO2D]             
          ], background: true
    
      )
    end
    

    And for queries

    $near command (without maxDistance)

     location = [80.24958300000003, 13.060422]
     items = Item.where(:loc => {"$near" => location})
    

    $near command (with maxDistance)

     distance = 10 #km
     location = [80.24958300000003, 13.060422]
     items = Item.where(:loc => {"$near" => location , '$maxDistance' => distance.fdiv(111.12)})
    

    Convert distance by 111.12 (one degree is approximately 111.12 kilometers) when using km, or leave distance as it is on using degree

    $centerSphere / $nearSphere queries

    location = [80.24958300000003, 13.060422]
    items = Item.where(:loc => {"$within" => {"$centerSphere" => [location, (distance.fdiv(6371) )]}})
    

    This will find the items within the 10 km radius. Here we need to convert the distance/6371(earth radius) to get it work with km.

    $box (bounding box queries)

     first_loc = [80.24958300000003, 13.060422]
     second_loc = [81.24958300000003, 12.060422]
     items = Item.where(:loc => {"$within" => {"$box" => [first_loc, second_loc]}})
    

    This will help you to find the items within the given bounding box.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MongoDB has two popular ORMs for Rails: MongoID and MongoMapper. Are there CouchDB ORM:s
I have a collection of articles in MongoDB that has the following structure: {
I'm just learning MongoDB and MongoMapper. This is on Rails 3. I created a
I'm building a Rails application using MongoDB as the back-end and MongoMapper as the
Background I'm designing a Rails app to record research data. Most of it can
created a collection in MongoDB consisting of 11446615 documents. Each document has the following
I am trying to use MongoDB on Ubuntu 11.10 with CakePHP 1.3 using the
I have an object that has an instance of Doctrine\ODM\MongoDB\Query\Builder . When I serialize
MongoDB is fast, but only when your working set or index can fit into
What database can you recomend for such case: Very lot of inserts and updates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.