Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
struggling with geospatial queries on mongo

I have been struggling for a few days trying to get queries to work. At the moment my model looks like this:

class Geojson
  include Mongoid::Document

  field :type, type: String, default: 'Point'
  field :coordinates, type: Array

  index({coordinates: "2dsphere"}, { bits: 12}) 
end

The following query returns nil:

Geojson.find(:coordinates => {"$nearSphere" => [-70.1197340629727, 4.67071244438]})

These are the current instances in my database:

[#<Geojson _id: 61b7b21a9eb0c9ef0aa5626d, type: "Point", coordinates: [-74.13041168951031, 4.6638117]>,
#<Geojson _id: 61b7b2619eb0c9ef0aa5626e, type: "Point", coordinates: [-74.1213041168951, 4.5638117]>] 

I am able to query similar cases on mongosh with no issues, however I am not sure where the mistake is when doing it directly on rails.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I finally managed to make it work the following way: (for a 2d sphere index)

Geojson.where(:coordinates => {"$nearSphere" => [long, lat]}).to_a

Where longitude and latitude are the parameters received.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!