Created
September 19, 2012 17:13
-
-
Save electronicbites/3750861 to your computer and use it in GitHub Desktop.
sample code for testing spatial indexes with mongoid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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