I have written a chat app for the company I work in and I worked with the mgo driver for some time. Now we refactor the mgo to the official mongo driver. I have implemented GridFS to work with the chat files as they are not big and it simplifies the job. The previous mgo driver when saving files had a list of data which one of the fields was contentType (Great right?)
So after refactoring most of the services that were included in this task I have noticed that the new official mongo driver does not do this??
So I have decided to try and add this field manually but then I got to the point that I don't understand how I can?
Tried with options.GridFSUpload().SetMetadata(metadata)
but I don't understand the logic of it and the internet is really minimal with the result about the new mongo driver working in GO.
Anyone can give me a hint how to add custom fields to file docs? Like contentType!!
Really appreciate it.
This is an example of what I tried to do
// GridFSInsert -
func GridFSInsert(fileName string, data []byte, metadata ...bsonx.Elem) (primitive.ObjectID, error) {
checkMongoConnection(false)
var fileID primitive.ObjectID
bucket, bucketErr := gridFs.NewBucket(
Mongo.Client.Database(Mongo.DBName),
options.GridFSBucket().SetName(gridFSColName),
)
if bucketErr != nil {
return fileID, bucketErr
}
uploadStream, uploadStreamErr := bucket.OpenUploadStream(
fileName,
options.GridFSUpload().SetMetadata(metadata),
)
if uploadStreamErr != nil {
return fileID, uploadStreamErr
}
defer uploadStream.Close()
fileSize, writeErr := uploadStream.Write(data)
if writeErr != nil {
return fileID, writeErr
}
fileID = uploadStream.FileID
log.Printf("Write file to DB was succesful, File size: %d", fileSize)
return fileID, nil
}
Sorry if I missed something as I'm not that experienced with GO as I would like to.
Thanks for any help