This is the 20th day of my participation in the August Text Challenge.More challenges in August
Previous recommended blog posts:
MongoDB Middleware Tool MGM Introduction (1)
MongoDB Middleware Tool MGM Introduction (2)
preface
As we all know, MongoDB is a general, document-based distributed database, which is very suitable for the application scenarios of cloud native services. The previous article has briefly introduced the general use of the MongoDB database middleware tool MGM, today we will talk about the advanced use of MGM.
The body of the
Default entries in database records
When declaring the database model, using DefaultModel will generate three default entries, _id, CREATED_AT, and updated_AT, when the records are generated in the database. Where, _id indicates the ID of the document record, created_at indicates the time when the document record is generated, and updated_at indicates the time when the document record is updated. Generally, updated_AT is at least equal to or newer than created_AT.
Next, take a look at these defaults by declaring a database model structure.
The code is as follows:
type File struct {
mgm.DefaultModel `bson:",inline"` // Generate three default entries
Name string `json:"name" bson:"name"`
Md5 string `json:"md5" bson:"md5"`
Type string `json:"type" bson:"type"`
Url string `json:"url" bson:"url"`
Path string `json:"path" bson:"path"`
FilePath string `json:"filepath" bson:"filepath"`
Duration float64 `json:"duration" bson:"duration"`
CloudUrl string `json:"cloudurl" bson:"cloudurl"`
Filekey string `json:"filekey" bson:"filekey"`
MinioUrl string `json:"miniourl" bson:"miniourl"`
MinioKey string `json:"miniokey" bson:"miniokey"`
Size int64 `json:"size" bson:"size"`
Identifier string `json:"identifier" bson:"identifier"`
ETag string `josn:"eTag" bson:"eTag"`
}
Copy the code
Screenshots of several database records are as follows:
From the figure above, you can see the three default fields in the database record (_id, CREATED_AT, updated_AT).
Hook functions in database records
Each model declared in the database is the following hook function:
Creating
: Triggered when a new model is created.Created
: triggered when a new model is created.Updating
: Triggered when a model is being updated.Updated
: Triggered when a model is updated.Saving
: Triggered when a model is created or upgraded.Saved
: Triggered when a model is created or upgraded.Deleting
: Triggered when a model is deleted.Deleted
: Triggered when a model is deleted.
Every database model triggers the Creating and Saving hook functions by default. If you want to define your own hook functions, use the default hook functions as well, such as the following code example:
func (model *Book) Creating(a) error {
// Call the default Creating hook function
iferr := model.DefaultModel.Creating(); err! =nil {
return err
}
// Define your own validation logic
if model.Pages < 0 {
return errors.New("page paramter error")}return nil
}
Copy the code
In addition, there are many methods that trigger the above hook functions, such as Create, CreateWithCtx, Update, UpdateWithCtx, Delete, DeleteWithCtx.
Database configuration Items
Here we need to explain a very important database configuration item – context timeout, this in the actual use of the process, I have repeatedly stepped on the pit, especially when the network is very poor, when accessing the database is always inexplicable connection failure, and finally locate the original problem here.
Therefore, it is recommended that when we use MGM, we must set an appropriate context timeout. The code example of my project is as follows:
func SetupMongoDB(a) error {
err := mgm.SetDefaultConfig(&mgm.Config{CtxTimeout: 10 * time.Second}, *config.GetConfig().Mongo.Db,
options.Client().ApplyURI(*config.GetConfig().Mongo.Url))
return err
}
Copy the code
At the end
Well, today on the advanced use of MGM on the introduction of so much, good night 😴! Hello, I’m Liuzhen007, welcome to share more server and database knowledge.