Class: Monga::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/monga/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, collection_name) ⇒ Collection

Returns a new instance of Collection.



5
6
7
8
# File 'lib/monga/collection.rb', line 5

def initialize(db, collection_name)
  @db = db
  @collection_name = collection_name
end

Instance Attribute Details

#collection_nameObject (readonly)

Returns the value of attribute collection_name.



3
4
5
# File 'lib/monga/collection.rb', line 3

def collection_name
  @collection_name
end

Instance Method Details

#aggregate(pipeline, &blk) ⇒ Object



146
147
148
149
150
151
152
153
154
155
# File 'lib/monga/collection.rb', line 146

def aggregate(pipeline, &blk)
  @db.aggregate(@collection_name, pipeline) do |err, resp|
    if block_given?
      yield(err, resp)
    else
      raise err if err
      return resp
    end
  end
end

#count(opts = {}) ⇒ Object

You could pass query/limit/skip options

count(query: {artist: "Madonna"}, limit: 10, skip: 0)


124
125
126
127
128
129
130
131
132
133
# File 'lib/monga/collection.rb', line 124

def count(opts = {})
  @db.count(@collection_name, opts) do |err, resp|
    if block_given?
      yield(err, resp)
    else
      raise err if err
      return resp
    end
  end
end

#delete(query = {}, opts = {}) ⇒ Object Also known as: remove



58
59
60
61
62
63
# File 'lib/monga/collection.rb', line 58

def delete(query = {}, opts = {})
  options = {}
  options[:query] = query
  options.merge!(opts)
  Monga::Protocol::Delete.new(connection, db_name, collection_name, options).perform
end

#distinct(opts, &blk) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'lib/monga/collection.rb', line 157

def distinct(opts, &blk)
  @db.distinct(collection_name, opts) do |err, resp|
    if block_given?
      yield(err, resp)
    else
      raise err if err
      return resp
    end
  end
end

#dropObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/monga/collection.rb', line 109

def drop
  @db.drop_collection(@collection_name) do |err, resp|
    if block_given?
      yield(err, resp)
    else
      raise err if err
      return resp
    end
  end
end

#drop_index(indexes) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/monga/collection.rb', line 76

def drop_index(indexes)
  @db.drop_indexes(@collection_name, indexes) do |err, resp|
    if block_given?
      yield(err, resp)
    else
      raise err if err
      return resp
    end
  end
end

#drop_indexesObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/monga/collection.rb', line 87

def drop_indexes
  @db.drop_indexes(@collection_name, "*") do |err, resp|
    if block_given?
      yield(err, resp)
    else
      raise err if err
      return resp
    end
  end
end

#ensure_index(keys, opts = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/monga/collection.rb', line 66

def ensure_index(keys, opts={})
  doc = {}
  doc[:key] = keys
  # Read docs about naming
  doc[:name] ||= keys.to_a.flatten * "_"
  doc[:ns] = "#{db_name}.#{collection_name}"
  doc.merge!(opts)
  Monga::Protocol::Insert.new(connection, db_name, "system.indexes", {documents: doc}).perform
end

#find_one(query = {}, selector = {}, opts = {}) ⇒ Object Also known as: first



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/monga/collection.rb', line 28

def find_one(query = {}, selector = {}, opts = {})
  options = {}
  options[:query] = query
  options[:selector] = selector
  options.merge!(opts)
  Monga::Cursor.create(connection, db_name, collection_name, options).first do |err, resp|
    if block_given?
      yield(err, resp)
    else
      err ? raise(err) : resp
    end
  end
end

#get_indexesObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/monga/collection.rb', line 98

def get_indexes
  Monga::Cursor.create(connection, db_name, "system.indexes").all do |err, resp|
    if block_given?
      yield(err, resp)
    else
      raise err if err
      return resp
    end
  end
end

#group(opts, &blk) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'lib/monga/collection.rb', line 168

def group(opts, &blk)
  @db.group(collection_name, opts) do |err, resp|
    if block_given?
      yield(err, resp)
    else
      raise err if err
      return resp
    end
  end
end

#insert(document, opts = {}) ⇒ Object



43
44
45
46
47
48
# File 'lib/monga/collection.rb', line 43

def insert(document, opts = {})
  options = {}
  options[:documents] = document
  options.merge!(opts)
  Monga::Protocol::Insert.new(connection, db_name, collection_name, options).perform
end

#map_reduce(opts, &blk) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/monga/collection.rb', line 135

def map_reduce(opts, &blk)
  @db.map_reduce(@collection_name, opts) do |err, resp|
    if block_given?
      yield(err, resp)
    else
      raise err if err
      return resp
    end
  end
end

#query(query = {}, selector = {}, opts = {}) ⇒ Object Also known as: find

Querying database. It returns cursor. Alias to collection#query is collection#find

cursor = collection.find(title: "Madonna")
# choose fields to return
cursor = collection.find({ title: "Madonna" }, { track: 1 })
# get all documents
cursor.all{ |err, docs| docs.each{ |doc| puts doc } }


19
20
21
22
23
24
25
# File 'lib/monga/collection.rb', line 19

def query(query = {}, selector = {}, opts = {})
  options = {}
  options[:query] = query
  options[:selector] = selector
  options.merge!(opts)
  Monga::Cursor.create(connection, db_name, collection_name, options)
end

#text(search, opts = {}, &blk) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/monga/collection.rb', line 179

def text(search, opts = {}, &blk)
  opts[:search] = search
  @db.text(collection_name, opts) do |err, resp|
    if block_given?
      yield(err, resp)
    else
      raise err if err
      return resp
    end
  end
end

#update(query = {}, update = {}, flags = {}) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/monga/collection.rb', line 50

def update(query = {}, update = {}, flags = {})
  options = {}
  options[:query] = query
  options[:update] = update
  options.merge!(flags)
  Monga::Protocol::Update.new(connection, db_name, collection_name, options).perform
end