Class: Mongo::Index::View
- Inherits:
-
Object
- Object
- Mongo::Index::View
- Extended by:
- Forwardable
- Includes:
- Enumerable, Retryable
- 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.
Methods included from Retryable
#legacy_write_with_retry, #nro_write_with_retry, #read_with_one_retry, #read_with_retry, #read_with_retry_cursor, #write_with_retry
Constructor Details
#initialize(collection, options = {}) ⇒ View
Create the new index view.
221 222 223 224 225 |
# File 'lib/mongo/index/view.rb', line 221 def initialize(collection, = {}) @collection = collection @batch_size = [:batch_size] @options = end |
Instance Attribute Details
#batch_size ⇒ Integer (readonly)
Returns batch_size The size of the batch of results when sending the listIndexes command.
31 32 33 |
# File 'lib/mongo/index/view.rb', line 31 def batch_size @batch_size end |
#collection ⇒ Collection (readonly)
Returns collection The indexes collection.
27 28 29 |
# File 'lib/mongo/index/view.rb', line 27 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.
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/mongo/index/view.rb', line 151 def create_many(*models) client.send(:with_session, @options) do |session| server = next_primary(nil, session) spec = { indexes: normalize_models(models.flatten, server), db_name: database.name, coll_name: collection.name, session: session } spec[:write_concern] = write_concern if server.features.collation_enabled? Operation::CreateIndex.new(spec).execute(server, client: client) end 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.
130 131 132 |
# File 'lib/mongo/index/view.rb', line 130 def create_one(keys, = {}) create_many({ key: keys }.merge()) end |
#drop_all ⇒ Result
Drop all indexes on the collection.
95 96 97 |
# File 'lib/mongo/index/view.rb', line 95 def drop_all drop_by_name(Index::ALL) end |
#drop_one(name) ⇒ Result
Drop an index by its name.
82 83 84 85 |
# File 'lib/mongo/index/view.rb', line 82 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.
193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/mongo/index/view.rb', line 193 def each(&block) session = client.send(:get_session, @options) cursor = read_with_retry_cursor(session, ServerSelector.primary, self) do |server| send_initial_query(server, session) end if block_given? cursor.each do |doc| yield doc end else cursor.to_enum end end |