Module: Mongo::CollectionExt

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

Instance Method Summary collapse

Instance Method Details

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



60
61
62
63
64
65
66
67
68
# File 'lib/mongo_db/driver/collection.rb', line 60

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

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



87
88
89
# File 'lib/mongo_db/driver/collection.rb', line 87

def count_with_ext selector = {}, opts = {}
  find(selector, opts).count()
end

#create(*args) ⇒ Object



42
43
44
# File 'lib/mongo_db/driver/collection.rb', line 42

def create *args
  insert *args
end

#destroy(*args) ⇒ Object



38
39
40
# File 'lib/mongo_db/driver/collection.rb', line 38

def destroy *args
  remove *args
end

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mongo_db/driver/collection.rb', line 70

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

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

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

Querying



49
50
51
52
53
54
# File 'lib/mongo_db/driver/collection.rb', line 49

def first selector = {}, opts = {}
  selector = convert_underscore_to_dollar_in_selector selector if selector.is_a? Hash

  h = find_one selector, opts
  symbolize_doc h
end

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



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

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

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



12
13
14
15
16
17
# File 'lib/mongo_db/driver/collection.rb', line 12

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

  # 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 = {}, opts = {}) ⇒ Object



33
34
35
36
# File 'lib/mongo_db/driver/collection.rb', line 33

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

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

CRUD



8
9
10
# File 'lib/mongo_db/driver/collection.rb', line 8

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

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



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongo_db/driver/collection.rb', line 19

def update_with_ext selector, doc, opts = {}
  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
  opts = if doc.keys.any?{|k| k =~ /^\$/}
    reverse_merge_defaults(opts, :safe, :multi)
  else
    reverse_merge_defaults(opts, :safe)
  end

  update_without_ext selector, doc, opts
end