Class: Mongoid::Contextual::GeoNear

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Command
Defined in:
build/mongoid-7.3/lib/mongoid/contextual/geo_near.rb

Instance Attribute Summary

Attributes included from Command

#collection, #collection The collection to query against., #criteria, #criteria The criteria for the context.

Instance Method Summary collapse

Methods included from Command

#client, #command

Constructor Details

#initialize(collection, criteria, near) ⇒ GeoNear

Initialize the new map/reduce directive.

Examples:

Initialize the new map/reduce.

MapReduce.new(criteria, map, reduce)

Parameters:

  • collection (Mongo::Collection)

    The collection to run the operation on.

  • criteria (Criteria)

    The Mongoid criteria.

  • near (String)

Since:

  • 3.0.0



75
76
77
78
79
80
# File 'build/mongoid-7.3/lib/mongoid/contextual/geo_near.rb', line 75

def initialize(collection, criteria, near)
  @collection, @criteria = collection, criteria
  command[:geoNear] = collection.name.to_s
  command[:near] = near
  apply_criteria_options
end

Instance Method Details

#average_distanceFloat?

Get the average distance for all documents from the point in the command.

Examples:

Get the average distance.

geo_near.average_distance

Returns:

  • (Float, nil)

    The average distance.

Since:

  • 3.1.0



23
24
25
26
# File 'build/mongoid-7.3/lib/mongoid/contextual/geo_near.rb', line 23

def average_distance
  average = stats["avgDistance"]
  (average.nil? || average.nan?) ? nil : average
end

#distance_multiplier(value) ⇒ GeoNear

Provide a distance multiplier to be used for each returned distance.

Examples:

Provide the distance multiplier.

geo_near.distance_multiplier(13113.1)

Parameters:

  • value (Integer, Float)

    The distance multiplier.

Returns:

  • (GeoNear)

    The GeoNear wrapper.

Since:

  • 3.1.0



59
60
61
62
# File 'build/mongoid-7.3/lib/mongoid/contextual/geo_near.rb', line 59

def distance_multiplier(value)
  command[:distanceMultiplier] = value
  self
end

#eachEnumerator

Iterates over each of the documents in the $geoNear, excluding the extra information that was passed back from the database.

Examples:

Iterate over the results.

geo_near.each do |doc|
  p doc
end

Returns:

  • (Enumerator)

    The enumerator.

Since:

  • 3.1.0



39
40
41
42
43
44
45
46
47
# File 'build/mongoid-7.3/lib/mongoid/contextual/geo_near.rb', line 39

def each
  if block_given?
    documents.each do |doc|
      yield doc
    end
  else
    to_enum
  end
end

#empty_and_chainable?true

Is this context’s criteria considered empty?

Examples:

Is this context’s criteria considered empty?

geo_near.empty_and_chainable?

Returns:

  • (true)

    Always true.

Since:

  • 5.1.0



213
214
215
# File 'build/mongoid-7.3/lib/mongoid/contextual/geo_near.rb', line 213

def empty_and_chainable?
  true
end

#executeHash

Execute the $geoNear, returning the raw output.

Examples:

Run the $geoNear

geo_near.execute

Returns:

  • (Hash)

    The raw output

Since:

  • 3.1.0



177
178
179
# File 'build/mongoid-7.3/lib/mongoid/contextual/geo_near.rb', line 177

def execute
  results
end

#inspectString

Get a pretty string representation of the command.

Examples:

Inspect the geoNear.

geo_near.inspect

Returns:

  • (String)

    The inspection string.

Since:

  • 3.1.0



90
91
92
93
94
95
96
97
98
99
100
101
# File 'build/mongoid-7.3/lib/mongoid/contextual/geo_near.rb', line 90

def inspect
%Q{#<Mongoid::Contextual::GeoNear
  selector:   #{criteria.selector.inspect}
  class:      #{criteria.klass}
  near:       #{command[:near]}
  multiplier: #{command[:distanceMultiplier] || "N/A"}
  max:        #{command[:maxDistance] || "N/A"}
  min:        #{command[:minDistance] || "N/A"}
  unique:     #{command[:unique].nil? ? true : command[:unique]}
  spherical:  #{command[:spherical] || false}>
}
end

#max_distance(value = nil) ⇒ GeoNear, Float

Specify the maximum distance to find documents for, or get the value of the document with the furthest distance.

Examples:

Set the max distance.

geo_near.max_distance(0.5)

Get the max distance.

geo_near.max_distance

Parameters:

  • value (Integer, Float) (defaults to: nil)

    The maximum distance.

Returns:

  • (GeoNear, Float)

    The GeoNear command or the value.

Since:

  • 3.1.0



117
118
119
120
121
122
123
124
# File 'build/mongoid-7.3/lib/mongoid/contextual/geo_near.rb', line 117

def max_distance(value = nil)
  if value
    command[:maxDistance] = value
    self
  else
    stats["maxDistance"]
  end
end

#min_distance(value) ⇒ GeoNear

Specify the minimum distance to find documents for.

Examples:

Set the min distance.

geo_near.min_distance(0.5)

Parameters:

  • value (Integer, Float)

    The minimum distance.

Returns:

  • (GeoNear)

    The GeoNear command.

Since:

  • 3.1.0



136
137
138
139
# File 'build/mongoid-7.3/lib/mongoid/contextual/geo_near.rb', line 136

def min_distance(value)
  command[:minDistance] = value
  self
end

#sphericalGeoNear

Tell the command to calculate based on spherical distances.

Examples:

Add the spherical flag.

geo_near.spherical

Returns:

Since:

  • 3.1.0



149
150
151
152
# File 'build/mongoid-7.3/lib/mongoid/contextual/geo_near.rb', line 149

def spherical
  command[:spherical] = true
  self
end

#statsHash

Get the stats for the command run.

Examples:

Get the stats.

geo_near.stats

Returns:

  • (Hash)

    The stats from the command run.

Since:

  • 3.1.0



189
190
191
# File 'build/mongoid-7.3/lib/mongoid/contextual/geo_near.rb', line 189

def stats
  results["stats"]
end

#timeFloat

Get the execution time of the command.

Examples:

Get the execution time.

geo_near.time

Returns:

  • (Float)

    The execution time.

Since:

  • 3.1.0



201
202
203
# File 'build/mongoid-7.3/lib/mongoid/contextual/geo_near.rb', line 201

def time
  stats["time"]
end

#unique(value = true) ⇒ GeoNear

Tell the command whether or not the retured results should be unique.

Examples:

Set the unique flag.

geo_near.unique(false)

Parameters:

  • value (true, false) (defaults to: true)

    Whether to return unique documents.

Returns:

Since:

  • 3.1.0



164
165
166
167
# File 'build/mongoid-7.3/lib/mongoid/contextual/geo_near.rb', line 164

def unique(value = true)
  command[:unique] = value
  self
end