Class: Moped::Collection
- Inherits:
-
Object
- Object
- Moped::Collection
- 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.
103 104 105 106 |
# File 'lib/moped/collection.rb', line 103 def initialize(database, name) @database = database @name = name.to_s end |
Instance Attribute Details
#database ⇒ Database
Returns The database for the collection.
18 19 20 |
# File 'lib/moped/collection.rb', line 18 def database @database end |
#name ⇒ Object
18 |
# File 'lib/moped/collection.rb', line 18 attr_reader :database, :name |
Instance Method Details
#aggregate(*pipeline) ⇒ Hash
Call aggregate function over the collection.
149 150 151 |
# File 'lib/moped/collection.rb', line 149 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.
28 29 30 |
# File 'lib/moped/collection.rb', line 28 def capped? database.command(collstats: name)["capped"] end |
#drop ⇒ Hash
Drop the collection.
40 41 42 43 44 45 46 47 |
# File 'lib/moped/collection.rb', line 40 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.
77 78 79 |
# File 'lib/moped/collection.rb', line 77 def find(selector = {}) Query.new(self, selector) end |
#indexes ⇒ Indexes
Access information about this collection’s indexes.
90 91 92 |
# File 'lib/moped/collection.rb', line 90 def indexes Indexes.new(database, name) end |
#insert(documents, flags = nil) ⇒ nil
Insert one or more documents into the collection.
124 125 126 127 128 129 130 131 |
# File 'lib/moped/collection.rb', line 124 def insert(documents, flags = nil) with_retry(cluster) do docs = documents.is_a?(Array) ? documents : [ documents ] cluster.with_primary do |node| node.insert(database.name, name, docs, write_concern, flags: flags || []) end end end |
#rename(to_name) ⇒ Hash
Rename the collection
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/moped/collection.rb', line 57 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.
161 162 163 |
# File 'lib/moped/collection.rb', line 161 def session database.session end |
#write_concern ⇒ Object
165 166 167 |
# File 'lib/moped/collection.rb', line 165 def write_concern session.write_concern end |