func (iv IndexView) CreateMany(ctx context.Context, models []IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error) {
	names := make([]string, 0, len(models))

	var indexes bsoncore.Document
	aidx, indexes := bsoncore.AppendArrayStart(indexes)

	for i, model := range models {
		if model.Keys == nil {
			return nil, fmt.Errorf("index model keys cannot be nil")
		}

		name, err := getOrGenerateIndexName(iv.coll.registry, model)
		if err != nil {
			return nil, err
		}

		names = append(names, name)

		keys, err := transformBsoncoreDocument(iv.coll.registry, model.Keys)
		if err != nil {
			return nil, err
		}

		var iidx int32
		iidx, indexes = bsoncore.AppendDocumentElementStart(indexes, strconv.Itoa(i))
		indexes = bsoncore.AppendDocumentElement(indexes, "key", keys)

		if model.Options == nil {
			model.Options = options.Index()
		}
		model.Options.SetName(name)

		optsDoc, err := iv.createOptionsDoc(model.Options)
		if err != nil {
			return nil, err
		}

		indexes = bsoncore.AppendDocument(indexes, optsDoc)

		indexes, err = bsoncore.AppendDocumentEnd(indexes, iidx)
		if err != nil {
			return nil, err
		}
	}

	indexes, err := bsoncore.AppendArrayEnd(indexes, aidx)
	if err != nil {
		return nil, err
	}

	sess := sessionFromContext(ctx)

	if sess == nil && iv.coll.client.sessionPool != nil {
		sess, err = session.NewClientSession(iv.coll.client.sessionPool, iv.coll.client.id, session.Implicit)
		if err != nil {
			return nil, err
		}
		defer sess.EndSession()
	}

	err = iv.coll.client.validSession(sess)
	if err != nil {
		return nil, err
	}

	wc := iv.coll.writeConcern
	if sess.TransactionRunning() {
		wc = nil
	}
	if !writeconcern.AckWrite(wc) {
		sess = nil
	}

	selector := makePinnedSelector(sess, iv.coll.writeSelector)

	option := options.MergeCreateIndexesOptions(opts...)

	op := operation.NewCreateIndexes(indexes).
		Session(sess).WriteConcern(wc).ClusterClock(iv.coll.client.clock).
		Database(iv.coll.db.name).Collection(iv.coll.name).CommandMonitor(iv.coll.client.monitor).
		Deployment(iv.coll.client.deployment).ServerSelector(selector)

	if option.MaxTime != nil {
		op.MaxTimeMS(int64(*option.MaxTime / time.Millisecond))
	}

	err = op.Execute(ctx)
	if err != nil {
		return nil, err
	}

	return names, nil
}
// CreateMany executes a createIndexes command to create multiple indexes on the collection and returns the names of
// the new indexes.
//
// For each IndexModel in the models parameter, the index name can be specified via the Options field. If a name is not
// given, it will be generated from the Keys document.
//
// The opts parameter can be used to specify options for this operation (see the options.CreateIndexesOptions
// documentation).
//
// For more information about the command, see https://docs.mongodb.com/manual/reference/command/createIndexes/.
CreateMany executes a createIndexes command to create multiple indexes on the collection and returns the names of
the new indexes.

For each IndexModel in the models parameter, the index name can be specified via the Options field. If a name is not
given, it will be generated from the Keys document.

The opts parameter can be used to specify options for this operation (see the options.CreateIndexesOptions
documentation).


// CreateMany executes a createIndexes command to create multiple indexes on the collection and returns the names of // the new indexes . // // For each IndexModel in the models parameter the index name can be specified via the Options field . If a name is not // given it will be generated from the Keys document . // // The opts parameter can be used to specify options for this operation ( see the options . CreateIndexesOptions // documentation ) . // // For more information about the command see https : // docs . mongodb . com / manual / reference / command / createIndexes / .
func (iv IndexView) CreateMany(ctx context.Context, models []IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error) {
	names := make([]string, 0, len(models))
	var indexes bsoncore.Document
	aidx, indexes := bsoncore.AppendArrayStart(indexes)
	for i, model := range models {
		if model.Keys == nil {
			return nil, fmt.Errorf("index model keys cannot be nil")
		}
		name, err := getOrGenerateIndexName(iv.coll.registry, model)
		if err != nil {
			return nil, err
		}
		names = append(names, name)
		keys, err := transformBsoncoreDocument(iv.coll.registry, model.Keys)
		if err != nil {
			return nil, err
		}
		var iidx int32
		iidx, indexes = bsoncore.AppendDocumentElementStart(indexes, strconv.Itoa(i))
		indexes = bsoncore.AppendDocumentElement(indexes, "key", keys)
		if model.Options == nil {
			model.Options = options.Index()
		}
		model.Options.SetName(name)
		optsDoc, err := iv.createOptionsDoc(model.Options)
		if err != nil {
			return nil, err
		}
		indexes = bsoncore.AppendDocument(indexes, optsDoc)
		indexes, err = bsoncore.AppendDocumentEnd(indexes, iidx)
		if err != nil {
			return nil, err
		}
	}
	indexes, err := bsoncore.AppendArrayEnd(indexes, aidx)
	if err != nil {
		return nil, err
	}
	sess := sessionFromContext(ctx)
	if sess == nil && iv.coll.client.sessionPool != nil {
		sess, err = session.NewClientSession(iv.coll.client.sessionPool, iv.coll.client.id, session.Implicit)
		if err != nil {
			return nil, err
		}
		defer sess.EndSession()
	}
	err = iv.coll.client.validSession(sess)
	if err != nil {
		return nil, err
	}
	wc := iv.coll.writeConcern
	if sess.TransactionRunning() {
		wc = nil
	}
	if !writeconcern.AckWrite(wc) {
		sess = nil
	}
	selector := makePinnedSelector(sess, iv.coll.writeSelector)
	option := options.MergeCreateIndexesOptions(opts...)
	op := operation.NewCreateIndexes(indexes).
		Session(sess).WriteConcern(wc).ClusterClock(iv.coll.client.clock).
		Database(iv.coll.db.name).Collection(iv.coll.name).CommandMonitor(iv.coll.client.monitor).
		Deployment(iv.coll.client.deployment).ServerSelector(selector)
	if option.MaxTime != nil {
		op.MaxTimeMS(int64(*option.MaxTime / time.Millisecond))
	}
	err = op.Execute(ctx)
	if err != nil {
		return nil, err
	}
	return names, nil
}
func ( iv IndexView ) CreateMany ( ctx context . Context , models [ ] IndexModel , opts ... * options . CreateIndexesOptions ) ( [ ] string , error ) { names := make ( [ ] string , 0 , len ( models ) ) var indexes bsoncore . Document aidx , indexes := bsoncore . AppendArrayStart ( indexes ) for i , model := range models { if model . Keys == nil { return nil , fmt . Errorf ( " " ) } name , err := getOrGenerateIndexName ( iv . coll . registry , model ) if err != nil { return nil , err } names = append ( names , name ) keys , err := transformBsoncoreDocument ( iv . coll . registry , model . Keys ) if err != nil { return nil , err } var iidx int32 iidx , indexes = bsoncore . AppendDocumentElementStart ( indexes , strconv . Itoa ( i ) ) indexes = bsoncore . AppendDocumentElement ( indexes , " " , keys ) if model . Options == nil { model . Options = options . Index ( ) } model . Options . SetName ( name ) optsDoc , err := iv . createOptionsDoc ( model . Options ) if err != nil { return nil , err } indexes = bsoncore . AppendDocument ( indexes , optsDoc ) indexes , err = bsoncore . AppendDocumentEnd ( indexes , iidx ) if err != nil { return nil , err } } indexes , err := bsoncore . AppendArrayEnd ( indexes , aidx ) if err != nil { return nil , err } sess := sessionFromContext ( ctx ) if sess == nil && iv . coll . client . sessionPool != nil { sess , err = session . NewClientSession ( iv . coll . client . sessionPool , iv . coll . client . id , session . Implicit ) if err != nil { return nil , err } defer sess . EndSession ( ) } err = iv . coll . client . validSession ( sess ) if err != nil { return nil , err } wc := iv . coll . writeConcern if sess . TransactionRunning ( ) { wc = nil } if ! writeconcern . AckWrite ( wc ) { sess = nil } selector := makePinnedSelector ( sess , iv . coll . writeSelector ) option := options . MergeCreateIndexesOptions ( opts ... ) op := operation . NewCreateIndexes ( indexes ) . Session ( sess ) . WriteConcern ( wc ) . ClusterClock ( iv . coll . client . clock ) . Database ( iv . coll . db . name ) . Collection ( iv . coll . name ) . CommandMonitor ( iv . coll . client . monitor ) . Deployment ( iv . coll . client . deployment ) . ServerSelector ( selector ) if option . MaxTime != nil { op . MaxTimeMS ( int64 ( * option . MaxTime / time . Millisecond ) ) } err = op . Execute ( ctx ) if err != nil { return nil , err } return names , nil }
CreateMany executes a createIndexes command to create multiple indexes on the collection and returns the names of
the new indexes.
CreateMany executes a createIndexes command to create multiple indexes on the collection and returns the names of the new indexes .