Class: MongoDoc::Contexts::Mongo
- Includes:
- Mongoid::Contexts::Ids, Mongoid::Contexts::Paging
- Defined in:
- lib/mongo_doc/contexts/mongo.rb
Constant Summary collapse
- AGGREGATE_REDUCE =
"function(obj, prev) { prev.count++; }"
- GROUP_REDUCE =
"function(obj, prev) { prev.group.push(obj); }"
- MAX_REDUCE =
"function(obj, prev) { if (prev.max == 'start') { prev.max = obj.[field]; } " + "if (prev.max < obj.[field]) { prev.max = obj.[field]; } }"
- MIN_REDUCE =
"function(obj, prev) { if (prev.min == 'start') { prev.min = obj.[field]; } " + "if (prev.min > obj.[field]) { prev.min = obj.[field]; } }"
- SUM_REDUCE =
"function(obj, prev) { if (prev.sum == 'start') { prev.sum = 0; } prev.sum += obj.[field]; }"
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#criteria ⇒ Object
readonly
Returns the value of attribute criteria.
Instance Method Summary collapse
-
#aggregate ⇒ Object
Aggregate the context.
-
#avg(field) ⇒ Object
Get the average value for the supplied field.
-
#count ⇒ Object
Get the count of matching documents in the database for the context.
-
#distinct(field) ⇒ Object
Gets an array of distinct values for the supplied field across the entire collection or the susbset given the criteria.
-
#empty? ⇒ Boolean
(also: #blank?)
Determine if the context is empty or blank given the criteria.
-
#execute(paginating = false) ⇒ Object
Execute the context.
-
#group ⇒ Object
Groups the context.
-
#grouped(start, field, reduce) ⇒ Object
Common functionality for grouping operations.
-
#initialize(criteria) ⇒ Mongo
constructor
Create the new mongo context.
-
#iterate(&block) ⇒ Object
Iterate over each
Document
in the results. -
#last ⇒ Object
Return the last result for the
Context
. -
#max(field) ⇒ Object
Return the max value for a field.
-
#min(field) ⇒ Object
Return the min value for a field.
-
#one ⇒ Object
(also: #first)
Return the first result for the
Context
. -
#sum(field) ⇒ Object
Sum the context.
Methods included from Mongoid::Contexts::Ids
Methods included from Mongoid::Contexts::Paging
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)
133 134 135 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 133 def initialize(criteria) @criteria = criteria end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
7 8 9 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 7 def cache @cache end |
#criteria ⇒ Object (readonly)
Returns the value of attribute criteria.
7 8 9 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 7 def criteria @criteria end |
Instance Method Details
#aggregate ⇒ Object
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
25 26 27 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 25 def aggregate collection.group([:fields], selector, { :count => 0 }, AGGREGATE_REDUCE, 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.
43 44 45 46 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 43 def avg(field) total = sum(field) total ? (total / count) : nil end |
#count ⇒ Object
Get the count of matching documents in the database for the context.
Example:
context.count
Returns:
An Integer
count of documents.
57 58 59 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 57 def count @count ||= collection.find(selector, ).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)
67 68 69 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 67 def distinct(field) collection.distinct(field, selector) end |
#empty? ⇒ Boolean Also known as: blank?
Determine if the context is empty or blank given the criteria. Will perform a quick find_one asking only for the id.
Example:
context.blank?
77 78 79 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 77 def empty? collection.find_one(selector, ).nil? 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:
mongo.execute
Returns:
An enumerable Cursor
.
94 95 96 97 98 99 100 101 102 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 94 def execute(paginating = false) cursor = collection.find(selector, ) if cursor @count = cursor.count if paginating cursor else [] end end |
#group ⇒ Object
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.
117 118 119 120 121 122 123 124 125 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 117 def group collection.group( [:fields], selector, { :group => [] }, GROUP_REDUCE, true ).collect {|docs| docs["group"] = MongoDoc::BSON.decode(docs["group"]); docs } 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.
245 246 247 248 249 250 251 252 253 254 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 245 def grouped(start, field, reduce) result = collection.group( nil, selector, { start => "start" }, reduce.gsub("[field]", field), true ) result.empty? ? nil : result.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 }
143 144 145 146 147 148 149 150 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 143 def iterate(&block) return caching(&block) if criteria.cached? if block_given? execute.each do |doc| yield doc end end end |
#last ⇒ Object
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.
163 164 165 166 167 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 163 def last sorting = [:sort] || [[:_id, :asc]] [:sort] = sorting.collect { |option| [ option[0], option[1].invert ] } collection.find_one(selector, ) 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.
185 186 187 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 185 def max(field) grouped(:max, field.to_s, MAX_REDUCE) 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.
205 206 207 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 205 def min(field) grouped(:min, field.to_s, MIN_REDUCE) end |
#one ⇒ Object Also known as: first
Return the first result for the Context
.
Example:
context.one
Return:
The first document in the collection.
218 219 220 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 218 def one collection.find_one(selector, ) 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.
239 240 241 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 239 def sum(field) grouped(:sum, field.to_s, SUM_REDUCE) end |