Module: Mongo::CollectionExt

Defined in:
lib/mongo/driver/collection.rb

Instance Method Summary collapse

Instance Method Details

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



64
65
66
67
68
69
70
71
72
# File 'lib/mongo/driver/collection.rb', line 64

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

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



90
91
92
93
# File 'lib/mongo/driver/collection.rb', line 90

def count_with_ext selector = {}, options = {}
  selector = convert_underscore_to_dollar_in_selector selector if selector.is_a? Hash
  find(selector, options).count()
end

#create(*args) ⇒ Object



49
50
51
# File 'lib/mongo/driver/collection.rb', line 49

def create *args
  insert *args
end

#delete(*args) ⇒ Object



45
46
47
# File 'lib/mongo/driver/collection.rb', line 45

def delete *args
  remove *args
end

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mongo/driver/collection.rb', line 74

def each selector = {}, options = {}, &block
  selector = convert_underscore_to_dollar_in_selector selector

  cursor = nil
  begin
    cursor = find selector, reverse_merge_defaults(options, :batch_size)
    cursor.each do |doc|
      block.call doc
    end
    nil
  ensure
    cursor.close if cursor
  end
  nil
end

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

Querying.



55
56
57
58
# File 'lib/mongo/driver/collection.rb', line 55

def first selector = {}, options = {}
  selector = convert_underscore_to_dollar_in_selector selector if selector.is_a? Hash
  find_one selector, options
end

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



60
61
62
# File 'lib/mongo/driver/collection.rb', line 60

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

#insert_with_ext(args, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mongo/driver/collection.rb', line 11

def insert_with_ext args, options = {}
  result = insert_without_ext args, reverse_merge_defaults(options, :safe)

  # For some strange reason MongoDB Ruby driver
  # uses Strings for all keys but _id.
  # It's inconvinient, fixing it.
  if Mongo.defaults[:convert_id_to_string]
    list = args.is_a?(Array) ? args : [args]
    list.each{|h| h['_id'] = h.delete :_id}
  end

  # Fix for mongodriver, it will return single result if we supply [doc] as args.
  (args.is_a?(Array) and !result.is_a?(Array)) ? [result] : result
end

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



40
41
42
43
# File 'lib/mongo/driver/collection.rb', line 40

def remove_with_ext selector = {}, options = {}
  selector = convert_underscore_to_dollar_in_selector selector
  remove_without_ext selector, reverse_merge_defaults(options, :safe, :multi)
end

#save_with_ext(doc, options = {}) ⇒ Object

CRUD.



7
8
9
# File 'lib/mongo/driver/collection.rb', line 7

def save_with_ext doc, options = {}
  save_without_ext doc, reverse_merge_defaults(options, :safe)
end

#update_with_ext(selector, doc, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mongo/driver/collection.rb', line 26

def update_with_ext selector, doc, options = {}
  selector = convert_underscore_to_dollar_in_selector selector
  doc      = convert_underscore_to_dollar_in_update doc

  # because :multi works only with $ operators, we need to check if it's applicable
  options = if doc.keys.any?{|k| k =~ /^\$/}
    reverse_merge_defaults(options, :safe, :multi)
  else
    reverse_merge_defaults(options, :safe)
  end

  update_without_ext selector, doc, options
end