Skip to content

Instantly share code, notes, and snippets.

@electronicbites
Created September 19, 2012 17:13
Show Gist options
  • Save electronicbites/3750861 to your computer and use it in GitHub Desktop.
Save electronicbites/3750861 to your computer and use it in GitHub Desktop.
sample code for testing spatial indexes with mongoid
class Geo
include Mongoid::Document
BERLIN_LOCATION = [52.51171, 13.44325]
index({ 'location' => '2d' })
field :location, type: Array #0: latitude, 1: longitude
scope :nearby, ->(location, distance) { where('location' => {'$within' =>
{ '$center' => [location, distance], '$uniqueDocs' => true }})}
scope :berlin, -> {nearby(BERLIN_LOCATION, 2)}
end
require 'spec_helper'
describe Geo do
it 'test geoindex' do
geo = Geo.create!(location: [40.739257, -74.03273999999999])
Geo.berlin.to_a.should_not include geo
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment