Class: MongoDoc::Contexts::Mongo

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Contexts::Paging
Defined in:
lib/mongodoc/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

Instance Method Summary collapse

Methods included from Mongoid::Contexts::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)



106
107
108
# File 'lib/mongodoc/contexts/mongo.rb', line 106

def initialize(criteria)
  @criteria = criteria
end

Instance Attribute Details

#criteriaObject (readonly)

Returns the value of attribute criteria.



5
6
7
# File 'lib/mongodoc/contexts/mongo.rb', line 5

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



23
24
25
# File 'lib/mongodoc/contexts/mongo.rb', line 23

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

#countObject

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

Example:

context.count

Returns:

An Integer count of documents.



36
37
38
# File 'lib/mongodoc/contexts/mongo.rb', line 36

def count
  @count ||= collection.find(selector, options).count
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.



52
53
54
55
56
57
58
59
60
# File 'lib/mongodoc/contexts/mongo.rb', line 52

def execute(paginating = false)
  cursor = collection.find(selector, 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.



75
76
77
78
79
80
81
82
83
# File 'lib/mongodoc/contexts/mongo.rb', line 75

def group
  collection.group(
    options[: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.



203
204
205
206
207
208
209
210
211
212
# File 'lib/mongodoc/contexts/mongo.rb', line 203

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

#id_criteria(params) ⇒ Object

Return documents based on an id search. Will handle if a single id has been passed or mulitple ids.

Example:

context.id_criteria([1, 2, 3])

Returns:

The single or multiple documents.



95
96
97
98
# File 'lib/mongodoc/contexts/mongo.rb', line 95

def id_criteria(params)
  criteria.id(params)
  params.is_a?(Array) ? criteria.entries : one
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.



121
122
123
124
125
# File 'lib/mongodoc/contexts/mongo.rb', line 121

def last
  sorting = options[:sort] || [[:_id, :asc]]
  options[:sort] = sorting.collect { |option| [ option[0], option[1].invert ] }
  collection.find_one(selector, options)
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.



143
144
145
# File 'lib/mongodoc/contexts/mongo.rb', line 143

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.



163
164
165
# File 'lib/mongodoc/contexts/mongo.rb', line 163

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

#oneObject Also known as: first

Return the first result for the Context.

Example:

context.one

Return:

The first document in the collection.



176
177
178
# File 'lib/mongodoc/contexts/mongo.rb', line 176

def one
  collection.find_one(selector, options)
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.



197
198
199
# File 'lib/mongodoc/contexts/mongo.rb', line 197

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