Module: Mongo::Model::QueryMixin::ClassMethods

Includes:
DynamicFinders
Defined in:
lib/mongo/model/query_mixin.rb

Instance Method Summary collapse

Instance Method Details

#all(selector = {}, options = {}, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/mongo/model/query_mixin.rb', line 26

def all selector = {}, options = {}, &block
  if block
    each selector, options, &block
  else
    list = []
    each(selector, options){|doc| list << doc}
    list
  end
end

#count(selector = {}, options = {}) ⇒ Object



10
11
12
# File 'lib/mongo/model/query_mixin.rb', line 10

def count selector = {}, options = {}
  collection.count selector, options
end

#each(selector = {}, options = {}, &block) ⇒ Object



22
23
24
# File 'lib/mongo/model/query_mixin.rb', line 22

def each selector = {}, options = {}, &block
  collection.each selector, options, &block
end

#exists?(selector = {}, options = {}) ⇒ Boolean Also known as: exist?

Returns:



40
41
42
# File 'lib/mongo/model/query_mixin.rb', line 40

def exists? selector = {}, options = {}
  count(selector, options) > 0
end

#first(selector = {}, options = {}) ⇒ Object



18
19
20
# File 'lib/mongo/model/query_mixin.rb', line 18

def first selector = {}, options = {}
  collection.first selector, options
end

#first!(selector = {}, options = {}) ⇒ Object



36
37
38
# File 'lib/mongo/model/query_mixin.rb', line 36

def first! selector = {}, options = {}
  first(selector, options) || raise(Mongo::NotFound, "document with selector #{selector} not found!")
end

#query(*args) ⇒ Object Also known as: where



45
46
47
48
49
50
51
52
# File 'lib/mongo/model/query_mixin.rb', line 45

def query *args
  if args.first.is_a? Mongo::Model::Query
    args.first
  else
    selector, options = args.first.is_a?(::Array) ? args.first : args
    Mongo::Model::Query.new self, (selector || {}), (options || {})
  end
end

#size(*args) ⇒ Object



14
15
16
# File 'lib/mongo/model/query_mixin.rb', line 14

def size *args
  count *args
end