diff --git a/mongo/database.go b/mongo/database.go index ebc574cbf4..b08cf2c9c7 100644 --- a/mongo/database.go +++ b/mongo/database.go @@ -110,7 +110,10 @@ func (db *Database) Name() string { return db.name } -// Collection gets a handle for a collection with the given name configured with the given CollectionOptions. +// Collection returns a handle for a collection with the given name and options. +// +// If the collection does not exist on the server, it will be created when a +// write operation is performed. func (db *Database) Collection(name string, opts ...options.Lister[options.CollectionOptions]) *Collection { return newCollection(db, name, opts...) } @@ -582,14 +585,15 @@ func (db *Database) Watch(ctx context.Context, pipeline interface{}, return newChangeStream(ctx, csConfig, pipeline, opts...) } -// CreateCollection executes a create command to explicitly create a new collection with the specified name on the -// server. If the collection being created already exists, this method will return a mongo.CommandError. This method -// requires driver version 1.4.0 or higher. +// CreateCollection immediately creates a new collection on the server with the +// specified name and options. // -// The opts parameter can be used to specify options for the operation (see the options.CreateCollectionOptions -// documentation). +// MongoDB versions < 7.0 will return an error if the collection already exists. +// MongoDB versions >= 7.0 will not return an error if an existing collection +// created with the same name and options already exists. // -// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/create/. +// For more information about the command, see +// https://www.mongodb.com/docs/manual/reference/command/create/. func (db *Database) CreateCollection(ctx context.Context, name string, opts ...options.Lister[options.CreateCollectionOptions]) error { args, err := mongoutil.NewOptions(opts...) if err != nil {