Module: Mongoid::Geospatial::ClassMethods

Defined in:
lib/mongoid/geospatial.rb

Overview

Methods applied to Document’s class

Instance Method Summary collapse

Instance Method Details

#spatial_index(name, options = {}) ⇒ Object

Create Spatial index for given field

www.mongodb.org/display/DOCS/Geospatial+Indexing #GeospatialIndexing-geoNearCommand

Parameters:

  • name (String, Symbol)
  • options (Hash) (defaults to: {})

    options for spatial_index



84
85
86
87
# File 'lib/mongoid/geospatial.rb', line 84

def spatial_index(name, options = {})
  spatial_fields_indexed << name
  index({ name => '2d' }, options)
end

#spatial_scope(field, _opts = {}) ⇒ Object

Creates Sphere index for given field

www.mongodb.org/display/DOCS/Geospatial+Indexing #GeospatialIndexing-geoNearCommand

Parameters:

  • name (String, Symbol)
  • options (Hash)

    options for spatial_index



112
113
114
115
116
117
118
119
# File 'lib/mongoid/geospatial.rb', line 112

def spatial_scope(field, _opts = {})
  singleton_class.class_eval do
    # define_method(:close) do |args|
    define_method(:nearby) do |args|
      queryable.where(field.near_sphere => args)
    end
  end
end

#sphere_index(name, options = {}) ⇒ Object

Creates Sphere index for given field

www.mongodb.org/display/DOCS/Geospatial+Indexing #GeospatialIndexing-geoNearCommand

Parameters:

  • name (String, Symbol)
  • options (Hash) (defaults to: {})

    options for spatial_index



98
99
100
101
# File 'lib/mongoid/geospatial.rb', line 98

def sphere_index(name, options = {})
  spatial_fields_indexed << name
  index({ name => '2dsphere' }, options)
end