Class: Mongo::Index::View
- Inherits:
-
Object
- Object
- Mongo::Index::View
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/mongo/index/view.rb
Overview
A class representing a view of indexes.
Constant Summary collapse
- KEY =
The index key field.
'key'.freeze
- NAME =
The index name field.
'name'.freeze
- OPTIONS =
The mappings of Ruby index options to server options.
{ :background => :background, :bits => :bits, :bucket_size => :bucketSize, :default_language => :default_language, :expire_after => :expireAfterSeconds, :expire_after_seconds => :expireAfterSeconds, :key => :key, :language_override => :language_override, :max => :max, :min => :min, :name => :name, :partial_filter_expression => :partialFilterExpression, :sparse => :sparse, :sphere_version => :'2dsphereIndexVersion', :storage_engine => :storageEngine, :text_version => :textIndexVersion, :unique => :unique, :version => :v, :weights => :weights, :collation => :collation }.freeze
Instance Attribute Summary collapse
-
#batch_size ⇒ Integer
readonly
Batch_size The size of the batch of results when sending the listIndexes command.
-
#collection ⇒ Collection
readonly
Collection The indexes collection.
Instance Method Summary collapse
-
#create_many(*models) ⇒ Result
Creates multiple indexes on the collection.
-
#create_one(keys, options = {}) ⇒ Result
Creates an index on the collection.
-
#drop_all ⇒ Result
Drop all indexes on the collection.
-
#drop_one(name) ⇒ Result
Drop an index by its name.
-
#each(&block) ⇒ Object
Iterate over all indexes for the collection.
-
#get(keys_or_name) ⇒ Hash
Convenience method for getting index information by a specific name or spec.
-
#initialize(collection, options = {}) ⇒ View
constructor
Create the new index view.
Constructor Details
#initialize(collection, options = {}) ⇒ View
Create the new index view.
213 214 215 216 |
# File 'lib/mongo/index/view.rb', line 213 def initialize(collection, = {}) @collection = collection @batch_size = [:batch_size] end |
Instance Attribute Details
#batch_size ⇒ Integer (readonly)
Returns batch_size The size of the batch of results when sending the listIndexes command.
30 31 32 |
# File 'lib/mongo/index/view.rb', line 30 def batch_size @batch_size end |
#collection ⇒ Collection (readonly)
Returns collection The indexes collection.
26 27 28 |
# File 'lib/mongo/index/view.rb', line 26 def collection @collection end |
Instance Method Details
#create_many(*models) ⇒ Result
On MongoDB 3.0.0 and higher, the indexes will be created in parallel on the server.
Creates multiple indexes on the collection.
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/mongo/index/view.rb', line 150 def create_many(*models) server = next_primary spec = { indexes: normalize_models(models.flatten, server), db_name: database.name, coll_name: collection.name } spec[:write_concern] = write_concern if server.features.collation_enabled? Operation::Write::CreateIndex.new(spec).execute(server) end |
#create_one(keys, options = {}) ⇒ Result
Note that the options listed may be subset of those available.
Creates an index on the collection.
See the MongoDB documentation for a full list of supported options by server version.
129 130 131 |
# File 'lib/mongo/index/view.rb', line 129 def create_one(keys, = {}) create_many({ key: keys }.merge()) end |
#drop_all ⇒ Result
Drop all indexes on the collection.
94 95 96 |
# File 'lib/mongo/index/view.rb', line 94 def drop_all drop_by_name(Index::ALL) end |
#drop_one(name) ⇒ Result
Drop an index by its name.
81 82 83 84 |
# File 'lib/mongo/index/view.rb', line 81 def drop_one(name) raise Error::MultiIndexDrop.new if name == Index::ALL drop_by_name(name) end |
#each(&block) ⇒ Object
Iterate over all indexes for the collection.
190 191 192 193 194 195 196 197 |
# File 'lib/mongo/index/view.rb', line 190 def each(&block) server = next_primary(false) cursor = Cursor.new(self, send_initial_query(server), server).to_enum cursor.each do |doc| yield doc end if block_given? cursor end |