@@ -27,8 +27,9 @@ type collectionData struct {
2727}
2828
2929type createOptions struct {
30- Capped * bool `bson:"capped"`
31- SizeInBytes * int64 `bson:"size"`
30+ Capped * bool `bson:"capped"`
31+ SizeInBytes * int64 `bson:"size"`
32+ EncryptedFields any `bson:"encryptedFields"`
3233}
3334
3435// createCollection configures the collection represented by the receiver using the internal client. This function
@@ -49,14 +50,15 @@ func (c *collectionData) createCollection(ctx context.Context) error {
4950 if c .Options .SizeInBytes != nil {
5051 createOpts = createOpts .SetSizeInBytes (* c .Options .SizeInBytes )
5152 }
53+ if c .Options .EncryptedFields != nil {
54+ createOpts = createOpts .SetEncryptedFields (c .Options .EncryptedFields )
55+ }
5256
5357 if err := db .CreateCollection (ctx , c .CollectionName , createOpts ); err != nil {
5458 return fmt .Errorf ("error creating collection: %w" , err )
5559 }
56- }
57-
58- // If neither documents nor options are provided, still create the collection with write concern "majority".
59- if len (c .Documents ) == 0 && c .Options == nil {
60+ } else {
61+ // If options are provided, still create the collection with write concern "majority".
6062 // The write concern has to be manually specified in the command document because RunCommand does not honor
6163 // the database's write concern.
6264 create := bson.D {
@@ -68,13 +70,15 @@ func (c *collectionData) createCollection(ctx context.Context) error {
6870 if err := db .RunCommand (ctx , create ).Err (); err != nil {
6971 return fmt .Errorf ("error creating collection: %w" , err )
7072 }
71- return nil
7273 }
7374
74- docs := bsonutil .RawToInterfaces (c .Documents ... )
75- if _ , err := coll .InsertMany (ctx , docs ); err != nil {
76- return fmt .Errorf ("error inserting data: %w" , err )
75+ if len (c .Documents ) != 0 {
76+ docs := bsonutil .RawToInterfaces (c .Documents ... )
77+ if _ , err := coll .InsertMany (ctx , docs ); err != nil {
78+ return fmt .Errorf ("error inserting data: %w" , err )
79+ }
7780 }
81+
7882 return nil
7983}
8084
0 commit comments