// Create an index if it doesn't already exist
Create an index if it doesn't already exist
// Create an index if it doesn ' t already exist
func createIndexIfNotExists(ctx context.Context, iv mongo.IndexView, model mongo.IndexModel) error {
	c, err := iv.List(ctx)
	if err != nil {
		return err
	}
	defer func() {
		_ = c.Close(ctx)
	}()
	var found bool
	for c.Next(ctx) {
		keyElem, err := c.Current.LookupErr("key")
		if err != nil {
			return err
		}
		keyElemDoc := keyElem.Document()
		modelKeysDoc, err := bson.Marshal(model.Keys)
		if err != nil {
			return err
		}
		if bytes.Equal(modelKeysDoc, keyElemDoc) {
			found = true
			break
		}
	}
	if !found {
		_, err = iv.CreateOne(ctx, model)
		if err != nil {
			return err
		}
	}
	return nil
}
func createIndexIfNotExists ( ctx context . Context , iv mongo . IndexView , model mongo . IndexModel ) error { c , err := iv . List ( ctx ) if err != nil { return err } defer func ( ) { _ = c . Close ( ctx ) } ( ) var found bool for c . Next ( ctx ) { keyElem , err := c . Current . LookupErr ( " " ) if err != nil { return err } keyElemDoc := keyElem . Document ( ) modelKeysDoc , err := bson . Marshal ( model . Keys ) if err != nil { return err } if bytes . Equal ( modelKeysDoc , keyElemDoc ) { found = true break } } if ! found { _ , err = iv . CreateOne ( ctx , model ) if err != nil { return err } } return nil }
Create an index if it doesn't already exist
Create an index if it doesn ' t already exist