Class: Mongoid::Contexts::Mongo

Inherits:
Object
  • Object
show all
Includes:
Ids, Paging
Defined in:
lib/mongoid/contexts/mongo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ids

#id_criteria

Methods included from Paging

#page, #paginate, #per_page

Constructor Details

#initialize(criteria) ⇒ Mongo

Create the new mongo context. This will execute the queries given the selector and options against the database.

Example:

Mongoid::Contexts::Mongo.new(criteria)



134
135
136
137
138
139
140
141
# File 'lib/mongoid/contexts/mongo.rb', line 134

def initialize(criteria)
  @criteria = criteria
  if klass.hereditary
    criteria.in(:_type => criteria.klass._types)
  end
  criteria.enslave if klass.enslaved?
  criteria.cache if klass.cached?
end

Instance Attribute Details

#criteriaObject (readonly)

Returns the value of attribute criteria.



6
7
8
# File 'lib/mongoid/contexts/mongo.rb', line 6

def criteria
  @criteria
end

Instance Method Details

#aggregateObject

Aggregate the context. This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with counts.

Example:

context.aggregate

Returns:

A Hash with field values as keys, counts as values



22
23
24
# File 'lib/mongoid/contexts/mongo.rb', line 22

def aggregate
  klass.collection.group(options[:fields], selector, { :count => 0 }, Javascript.aggregate, true)
end

#avg(field) ⇒ Object

Get the average value for the supplied field.

This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with averages.

Example:

context.avg(:age)

Returns:

A numeric value that is the average.



40
41
42
43
# File 'lib/mongoid/contexts/mongo.rb', line 40

def avg(field)
  total = sum(field)
  total ? (total / count) : nil
end

#blank?Boolean Also known as: empty?

Determine if the context is empty or blank given the criteria. Will perform a quick has_one asking only for the id.

Example:

context.blank?

Returns:



51
52
53
# File 'lib/mongoid/contexts/mongo.rb', line 51

def blank?
  klass.collection.find_one(selector, { :fields => [ :_id ] }).nil?
end

#countObject

Get the count of matching documents in the database for the context.

Example:

context.count

Returns:

An Integer count of documents.



66
67
68
# File 'lib/mongoid/contexts/mongo.rb', line 66

def count
  @count ||= klass.collection.find(selector, process_options).count
end

#distinct(field) ⇒ Object

Gets an array of distinct values for the supplied field across the entire collection or the susbset given the criteria.

Example:

context.distinct(:title)



76
77
78
# File 'lib/mongoid/contexts/mongo.rb', line 76

def distinct(field)
  klass.collection.distinct(field, selector)
end

#execute(paginating = false) ⇒ Object

Execute the context. This will take the selector and options and pass them on to the Ruby driver’s find() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned new documents of the type of class provided will be instantiated.

Example:

context.execute

Returns:

An enumerable Cursor.



92
93
94
95
96
97
98
99
100
# File 'lib/mongoid/contexts/mongo.rb', line 92

def execute(paginating = false)
  cursor = klass.collection.find(selector, process_options)
  if cursor
    @count = cursor.count if paginating
    cursor
  else
    []
  end
end

#groupObject

Groups the context. This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with objects.

Example:

context.group

Returns:

A Hash with field values as keys, arrays of documents as values.



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/mongoid/contexts/mongo.rb', line 114

def group
  klass.collection.group(
    options[:fields],
    selector,
    { :group => [] },
    Javascript.group
  ).collect do |docs|
    docs["group"] = docs["group"].collect do |attrs|
      Mongoid::Factory.build(klass, attrs)
    end
    docs
  end
end

#grouped(start, field, reduce) ⇒ Object

Common functionality for grouping operations. Currently used by min, max and sum. Will gsub the field name in the supplied reduce function.



248
249
250
251
252
253
254
255
256
# File 'lib/mongoid/contexts/mongo.rb', line 248

def grouped(start, field, reduce)
  collection = klass.collection.group(
    nil,
    selector,
    { start => "start" },
    reduce.gsub("[field]", field)
  )
  collection.empty? ? nil : collection.first[start.to_s]
end

#iterate(&block) ⇒ Object

Iterate over each Document in the results. This can take an optional block to pass to each argument in the results.

Example:

context.iterate { |doc| p doc }



149
150
151
152
153
154
# File 'lib/mongoid/contexts/mongo.rb', line 149

def iterate(&block)
  return caching(&block) if criteria.cached?
  if block_given?
    execute.each { |doc| yield doc }
  end
end

#lastObject

Return the last result for the Context. Essentially does a find_one on the collection with the sorting reversed. If no sorting parameters have been provided it will default to ids.

Example:

context.last

Returns:

The last document in the collection.



167
168
169
170
171
172
173
174
# File 'lib/mongoid/contexts/mongo.rb', line 167

def last
  opts = process_options
  sorting = opts[:sort]
  sorting = [[:_id, :asc]] unless sorting
  opts[:sort] = sorting.collect { |option| [ option[0], option[1].invert ] }
  attributes = klass.collection.find_one(selector, opts)
  attributes ? Mongoid::Factory.build(klass, attributes) : nil
end

#max(field) ⇒ Object

Return the max value for a field.

This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with sums.

Example:

context.max(:age)

Returns:

A numeric max value.



190
191
192
# File 'lib/mongoid/contexts/mongo.rb', line 190

def max(field)
  grouped(:max, field.to_s, Javascript.max)
end

#min(field) ⇒ Object

Return the min value for a field.

This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with sums.

Example:

context.min(:age)

Returns:

A numeric minimum value.



208
209
210
# File 'lib/mongoid/contexts/mongo.rb', line 208

def min(field)
  grouped(:min, field.to_s, Javascript.min)
end

#oneObject Also known as: first

Return the first result for the Context.

Example:

context.one

Return:

The first document in the collection.



221
222
223
224
# File 'lib/mongoid/contexts/mongo.rb', line 221

def one
  attributes = klass.collection.find_one(selector, process_options)
  attributes ? Mongoid::Factory.build(klass, attributes) : nil
end

#process_optionsObject

Filters the field list. If no fields have been supplied, then it will be empty. If fields have been defined then _type will be included as well.



260
261
262
263
264
265
266
267
# File 'lib/mongoid/contexts/mongo.rb', line 260

def process_options
  fields = options[:fields]
  if fields && fields.size > 0 && !fields.include?(:_type)
    fields << :_type
    options[:fields] = fields
  end
  options.dup
end

#sum(field) ⇒ Object

Sum the context.

This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with sums.

Example:

context.sum(:age)

Returns:

A numeric value that is the sum.



242
243
244
# File 'lib/mongoid/contexts/mongo.rb', line 242

def sum(field)
  grouped(:sum, field.to_s, Javascript.sum)
end