Class: Moped::Collection
Overview
The class for interacting with a MongoDB collection.
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
- #database The collection's database.(Thecollection's database.) ⇒ Object readonly
-
#name ⇒ Object
readonly
Returns the value of attribute name.
- #name The collection name.(Thecollectionname.) ⇒ Object readonly
Instance Method Summary collapse
-
#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.
Constructor Details
#initialize(database, name) ⇒ Collection
Initialize the new collection.
68 69 70 |
# File 'lib/moped/collection.rb', line 68 def initialize(database, name) @database, @name = database, name.to_s end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
14 15 16 |
# File 'lib/moped/collection.rb', line 14 def database @database end |
#database The collection's database.(Thecollection's database.) ⇒ Object (readonly)
14 |
# File 'lib/moped/collection.rb', line 14 attr_reader :database, :name |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/moped/collection.rb', line 14 def name @name end |
#name The collection name.(Thecollectionname.) ⇒ Object (readonly)
14 |
# File 'lib/moped/collection.rb', line 14 attr_reader :database, :name |
Instance Method Details
#drop ⇒ Hash
Drop the collection.
24 25 26 27 28 29 30 |
# File 'lib/moped/collection.rb', line 24 def drop begin database.command(drop: name) rescue Moped::Errors::OperationFailure => e false end end |
#find(selector = {}) ⇒ Query Also known as: where
Build a query for this collection.
42 43 44 |
# File 'lib/moped/collection.rb', line 42 def find(selector = {}) Query.new(self, selector) end |
#indexes ⇒ Indexes
Access information about this collection’s indexes.
55 56 57 |
# File 'lib/moped/collection.rb', line 55 def indexes Indexes.new(database, name) end |
#insert(documents, flags = nil) ⇒ nil
Insert one or more documents into the collection.
88 89 90 91 92 93 |
# File 'lib/moped/collection.rb', line 88 def insert(documents, flags = nil) documents = [documents] unless documents.is_a?(Array) database.session.with(consistency: :strong) do |session| session.context.insert(database.name, name, documents, flags: flags || []) end end |