lib/mongo/index/view.rb
# Creates multiple indexes on the collection.
#
# @example Create multiple indexes.
#   view.create_many([
#     { key: { name: 1 }, unique: true },
#     { key: { age: -1 }, background: true }
#   ])
#
# @example Create multiple indexes with options.
#   view.create_many(
#     { key: { name: 1 }, unique: true },
#     { key: { age: -1 }, background: true },
#     { commit_quorum: 'majority' }
#   )
#
# @note On MongoDB 3.0.0 and higher, the indexes will be created in
#   parallel on the server.
#
# @param [ Array<Hash> ] models The index specifications. Each model MUST
#   include a :key option, except for the last item in the Array, which
#   may be a Hash specifying options relevant to the createIndexes operation.
#   The following options are accepted:
#   - commit_quorum: Specify how many data-bearing members of a replica set,
#     including the primary, must complete the index builds successfully
#     before the primary marks the indexes as ready. Potential values are:
#     - an integer from 0 to the number of members of the replica set
#     - "majority" indicating that a majority of data bearing nodes must vote
#     - "votingMembers" which means that all voting data bearing nodes must vote
#   - session: The session to use.
#
# @return [ Result ] The result of the command.
#
# @since 2.0.0
Creates multiple indexes on the collection.
Creates multiple indexes on the collection .
def create_many(*models)
        models = models.flatten
        options = {}
        if models && !models.last.key?(:key)
          options = models.pop
        end
        client.send(:with_session, @options.merge(options)) do |session|
          server = next_primary(nil, session)
          indexes = normalize_models(models, server)
          indexes.each do |index|
            if index[:bucketSize] || index['bucketSize']
              client.log_warn("Haystack indexes (bucketSize index option) are deprecated as of MongoDB 4.4")
            end
          end
          spec = {
            indexes: indexes,
            db_name: database.name,
            coll_name: collection.name,
            session: session,
            commit_quorum: options[:commit_quorum],
            write_concern: write_concern,
          }
          Operation::CreateIndex.new(spec).execute(server, context: Operation::Context.new(client: client, session: session))
        end
      end
def create_many ( * models ) models = models . flatten options = { } if models && ! models . last . key? ( :key ) options = models . pop end client . send ( :with_session , @options . merge ( options ) ) do | session | server = next_primary ( nil , session ) indexes = normalize_models ( models , server ) indexes . each do | index | if index [ :bucketSize ] || index [ 'bucketSize' ] client . log_warn ( "Haystack indexes (bucketSize index option) are deprecated as of MongoDB 4.4" ) end end spec = { indexes : indexes , db_name : database . name , coll_name : collection . name , session : session , commit_quorum : options [ :commit_quorum ] , write_concern : write_concern , } Operation :: CreateIndex . new ( spec ) . execute ( server , context : Operation :: Context . new ( client : client , session : session ) ) end end
Creates multiple indexes on the collection.
Creates multiple indexes on the collection .
The result of the command.
The result of the command .
 Result 
models
<not_specific>
<not_specific>
models The index specifications. Each model MUST include a :key option, except for the last item in the Array, which may be a Hash specifying options relevant to the createIndexes operation. The following options are accepted: - commit_quorum: Specify how many data-bearing members of a replica set, including the primary, must complete the index builds successfully before the primary marks the indexes as ready. Potential values are: - an integer from 0 to the number of members of the replica set - "majority" indicating that a majority of data bearing nodes must vote - "votingMembers" which means that all voting data bearing nodes must vote - session: The session to use.
models The index specifications . Each model MUST include a : key option except for the last item in the Array which may be a Hash specifying options relevant to the createIndexes operation . The following options are accepted : - commit_quorum : Specify how many data - bearing members of a replica set including the primary must complete the index builds successfully before the primary marks the indexes as ready . Potential values are : - an integer from 0 to the number of members of the replica set - " majority " indicating that a majority of data bearing nodes must vote - " votingMembers " which means that all voting data bearing nodes must vote - session : The session to use .
example
Create multiple indexes. view.create_many([ { key: { name: 1 }, unique: true }, { key: { age: -1 }, background: true } ])
Create multiple indexes . view . create_many ( [ { key : { name : 1 } unique : true } { key : { age : - 1 } background : true } ] )
example
Create multiple indexes with options. view.create_many( { key: { name: 1 }, unique: true }, { key: { age: -1 }, background: true }, { commit_quorum: 'majority' } )
Create multiple indexes with options . view . create_many ( { key : { name : 1 } unique : true } { key : { age : - 1 } background : true } { commit_quorum : ' majority ' } )
note
On MongoDB 3.0.0 and higher, the indexes will be created in parallel on the server.
On MongoDB 3 . 0 . 0 and higher the indexes will be created in parallel on the server .
since
None