lib/mongoid_spacial/contexts/mongo.rb
def create_geo_near_query(center,opts)
        # minimum query
        query = {
          :geoNear  => klass.collection_name,
          :near     => center,
        }

        # create limit and use skip
        if opts[:num]
          query['num']         = opts[:skip].to_i + opts[:num].to_i
        elsif opts[:limit]
          query['num']         = opts[:skip].to_i + opts[:limit].to_i
        elsif opts[:page]
          query['num'] = opts[:skip].to_i + (opts[:page].to_i * opts[:per_page].to_i)
        end

        # allow the use of complex werieis
        if opts[:query]
          query['query']         = self.criteria.where(opts[:query]).selector
        elsif self.selector != {}
          query['query']         = self.selector
        end

        if opts[:max_distance]
          query['maxDistance'] = opts[:max_distance].to_f
          query['maxDistance'] = query['maxDistance']/opts[:unit].to_f if opts[:unit]
        end

        if klass.db.connection.server_version >= '1.7'
          query['spherical']  = true if opts[:spherical]

          # mongodb < 1.7 returns degrees but with earth flat. in Mongodb 1.7 you can set sphere and let mongodb calculate the distance in Miles or KM
          # for mongodb < 1.7 we need to run Haversine first before calculating degrees to Km or Miles. See below.
          query['distanceMultiplier'] = opts[:distance_multiplier].to_f if opts[:distance_multiplier]
        end
        query
      end
# Fetches rows from the data base sorted by distance.
# In MongoDB versions 1.7 and above it returns a distance.
# Uses all criteria chains except without, only, asc, desc, order_by
#
# @example Minimal Query
#
#   Address.geo_near([70,40])
#
# @example Chained Query
#
#   Address.where(:state => 'ny').geo_near([70,40])
#
# @example Calc Distances Query
#
#   Address.geo_near([70,40], :max_distance => 5, :unit => 5)
#
# @param [ Array, Hash, #to_lng_lat ] center The center of where to calculate distance from
# @param [ Hash ] opts the options to query with
# @options opts [Integer] :num The number of rows to fetch
# @options opts [Hash] :query The query to filter the rows by, accepts
# @options opts [Numeric] :distance_multiplier this is multiplied against the calculated distance
# @options opts [Numeric] :max_distance The max distance of a row that should be returned in :unit(s)
# @options opts [Numeric, :km, :k, :mi, :ft] :unit automatically sets :distance_multiplier and converts :max_distance
# @options opts [true,false] :spherical Will determine the distance either by spherical calculation or flat calculation
# @options opts [TrueClass,Array<Symbol>] :calculate Which extra fields to calculate distance for in ruby, if set to TrueClass it will calculate all spacial fields
#
# @return [ Array ] Sorted Rows
Fetches rows from the data base sorted by distance.
In MongoDB versions 1.7 and above it returns a distance.
Uses all criteria chains except without, only, asc, desc, order_by
Fetches rows from the data base sorted by distance . In MongoDB versions 1 . 7 and above it returns a distance . Uses all criteria chains except without only asc desc order_by
def create_geo_near_query(center,opts)
        query = {
          :geoNear  => klass.collection_name,
          :near     => center,
        }
        if opts[:num]
          query['num']         = opts[:skip].to_i + opts[:num].to_i
        elsif opts[:limit]
          query['num']         = opts[:skip].to_i + opts[:limit].to_i
        elsif opts[:page]
          query['num'] = opts[:skip].to_i + (opts[:page].to_i * opts[:per_page].to_i)
        end
        if opts[:query]
          query['query']         = self.criteria.where(opts[:query]).selector
        elsif self.selector != {}
          query['query']         = self.selector
        end
        if opts[:max_distance]
          query['maxDistance'] = opts[:max_distance].to_f
          query['maxDistance'] = query['maxDistance']/opts[:unit].to_f if opts[:unit]
        end
        if klass.db.connection.server_version >= '1.7'
          query['spherical']  = true if opts[:spherical]
          query['distanceMultiplier'] = opts[:distance_multiplier].to_f if opts[:distance_multiplier]
        end
        query
      end
def create_geo_near_query ( center , opts ) query = { :geoNear => klass . collection_name , :near => center , } if opts [ :num ] query [ 'num' ] = opts [ :skip ] . to_i + opts [ :num ] . to_i elsif opts [ :limit ] query [ 'num' ] = opts [ :skip ] . to_i + opts [ :limit ] . to_i elsif opts [ :page ] query [ 'num' ] = opts [ :skip ] . to_i + ( opts [ :page ] . to_i * opts [ :per_page ] . to_i ) end if opts [ :query ] query [ 'query' ] = self . criteria . where ( opts [ :query ] ) . selector elsif self . selector != { } query [ 'query' ] = self . selector end if opts [ :max_distance ] query [ 'maxDistance' ] = opts [ :max_distance ] . to_f query [ 'maxDistance' ] = query [ 'maxDistance' ] / opts [ :unit ] . to_f if opts [ :unit ] end if klass . db . connection . server_version >= '1.7' query [ 'spherical' ] = true if opts [ :spherical ] query [ 'distanceMultiplier' ] = opts [ :distance_multiplier ] . to_f if opts [ :distance_multiplier ] end query end
Fetches rows from the data base sorted by distance.
Fetches rows from the data base sorted by distance .
# minimum query # create limit and use skip # allow the use of complex werieis # mongodb < 1.7 returns degrees but with earth flat. in Mongodb 1.7 you can set sphere and let mongodb calculate the distance in Miles or KM # for mongodb < 1.7 we need to run Haversine first before calculating degrees to Km or Miles. See below.
None
 Array 
center
opts
opts the options to query with
opts the options to query with
example
Minimal Query Address.geo_near([70,40])
Minimal Query Address . geo_near ( [ 70 40 ] )
example
Chained Query Address.where(:state => 'ny').geo_near([70,40])
Chained Query Address . where ( : state = > ' ny ' ) . geo_near ( [ 70 40 ] )
example
Calc Distances Query Address.geo_near([70,40], :max_distance => 5, :unit => 5)
Calc Distances Query Address . geo_near ( [ 70 40 ] : max_distance = > 5 : unit = > 5 )
options
opts [Integer] :num The number of rows to fetch
opts [ Integer ] : num The number of rows to fetch
options
opts [Hash] :query The query to filter the rows by, accepts
opts [ Hash ] : query The query to filter the rows by accepts
options
opts [Numeric] :distance_multiplier this is multiplied against the calculated distance
opts [ Numeric ] : distance_multiplier this is multiplied against the calculated distance
options
opts [Numeric] :max_distance The max distance of a row that should be returned in :unit(s)
opts [ Numeric ] : max_distance The max distance of a row that should be returned in : unit ( s )
options
opts [Numeric, :km, :k, :mi, :ft] :unit automatically sets :distance_multiplier and converts :max_distance
opts [ Numeric : km : k : mi : ft ] : unit automatically sets : distance_multiplier and converts : max_distance
options
opts [true,false] :spherical Will determine the distance either by spherical calculation or flat calculation
opts [ true false ] : spherical Will determine the distance either by spherical calculation or flat calculation
options
opts [TrueClass,Array] :calculate Which extra fields to calculate distance for in ruby, if set to TrueClass it will calculate all spacial fields
opts [ TrueClass Array ] : calculate Which extra fields to calculate distance for in ruby if set to TrueClass it will calculate all spacial fields