Class: Moped::Collection
- Inherits:
-
Object
- Object
- Moped::Collection
- Includes:
- Readable
- Defined in:
- lib/moped/collection.rb
Overview
The class for interacting with a MongoDB collection.
Instance Attribute Summary collapse
-
#database ⇒ Database
The database for the collection.
- #name ⇒ Object
Instance Method Summary collapse
-
#aggregate(*pipeline) ⇒ Hash
Call aggregate function over the collection.
-
#capped? ⇒ true, false
Return whether or not this collection is a capped collection.
-
#drop ⇒ Hash
Drop the collection.
-
#find(selector = {}) ⇒ Query
(also: #where)
Build a query for this collection.
-
#indexes ⇒ Indexes
Access information about this collection’s indexes.
-
#initialize(database, name) ⇒ Collection
constructor
Initialize the new collection.
-
#insert(documents, flags = nil) ⇒ nil
Insert one or more documents into the collection.
-
#rename(to_name) ⇒ Hash
Rename the collection.
-
#session ⇒ Session
Get the session for the collection.
- #write_concern ⇒ Object
Constructor Details
#initialize(database, name) ⇒ Collection
Initialize the new collection.
101 102 103 104 |
# File 'lib/moped/collection.rb', line 101 def initialize(database, name) @database = database @name = name.to_s end |
Instance Attribute Details
#database ⇒ Database
Returns The database for the collection.
16 17 18 |
# File 'lib/moped/collection.rb', line 16 def database @database end |
#name ⇒ Object
16 |
# File 'lib/moped/collection.rb', line 16 attr_reader :database, :name |
Instance Method Details
#aggregate(*pipeline) ⇒ Hash
Call aggregate function over the collection.
145 146 147 |
# File 'lib/moped/collection.rb', line 145 def aggregate(*pipeline) session.command(aggregate: name, pipeline: pipeline.flatten)["result"] end |
#capped? ⇒ true, false
Return whether or not this collection is a capped collection.
26 27 28 |
# File 'lib/moped/collection.rb', line 26 def capped? database.command(collstats: name)["capped"] end |
#drop ⇒ Hash
Drop the collection.
38 39 40 41 42 43 44 45 |
# File 'lib/moped/collection.rb', line 38 def drop begin session.with(read: :primary).command(drop: name) rescue Moped::Errors::OperationFailure => e raise e unless e.ns_not_found? false end end |
#find(selector = {}) ⇒ Query Also known as: where
Build a query for this collection.
75 76 77 |
# File 'lib/moped/collection.rb', line 75 def find(selector = {}) Query.new(self, selector) end |
#indexes ⇒ Indexes
Access information about this collection’s indexes.
88 89 90 |
# File 'lib/moped/collection.rb', line 88 def indexes Indexes.new(database, name) end |
#insert(documents, flags = nil) ⇒ nil
Insert one or more documents into the collection.
122 123 124 125 126 127 |
# File 'lib/moped/collection.rb', line 122 def insert(documents, flags = nil) docs = documents.is_a?(Array) ? documents : [ documents ] cluster.with_primary do |node| node.insert(database.name, name, docs, write_concern, flags: flags || []) end end |
#rename(to_name) ⇒ Hash
Rename the collection
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/moped/collection.rb', line 55 def rename(to_name) begin session. with(database: "admin", read: :primary). command(renameCollection: "#{database.name}.#{name}", to: "#{database.name}.#{to_name}") rescue Moped::Errors::OperationFailure => e raise e unless e.ns_not_exists? false end end |
#session ⇒ Session
Get the session for the collection.
157 158 159 |
# File 'lib/moped/collection.rb', line 157 def session database.session end |
#write_concern ⇒ Object
161 162 163 |
# File 'lib/moped/collection.rb', line 161 def write_concern session.write_concern end |