An awesome blog about software development and network security.
post @ 2020-12-22

Why migrate to qmgo

  • Mgo is a convenient library for golang to operate mongodb, but it does not support the latest features of mongodb, such as Transaction.
  • My old project is written in mgo. If I want to migrate to the official mongo-driver, its syntax is more primitive, the usage difference is large, and the migration cost will be higher.
  • I found a library called qmgo, which is based on the official mongo-driver package, but it is closer to mgo in terms of syntax and suitable for simple and rude migrations.

Below I will record the methods used in the migration process, which I found out by myself. If you have any questions, please correct me.

Batch replacement

  • Batch replace github.com/globalsign/mgo/bson with go.mongodb.org/mongo-driver/bson
  • Batch replace bson.NewObjectId() with primitive.NewObjectID()
  • Batch replace bson.ObjectId with primitive.ObjectID
  • Batch replace .Find(bson.M with .Find(c, bson.M
  • Batch replace .Find(search to .Find(c, search
  • Batch replace .Insert( to .InsertOne(c,
  • Batch replace .Update( to .UpdateOne(c,
  • Batch replace .RemoveAll( to .RemoveAll(c,
  • Batch replace .Remove( to .Remove(c,
  • Batch replace errors.Is(err, mgo.ErrNotFound) with qmgo.IsErrNoDocuments(err)
  • Batch replace .EnsureIndex(mgo.Index{ for
    .CreateOneIndex(c, options.IndexModel{

Copy and paste

  • Where there is no context, copy and paste everywhere c := context.Background()
  • If it is in the interface method of gin, you can directly use gin’s c *gin.Context (but if multi-coroutine operation needs to Copy gin’s Context)

Function substitution

  • Replace the bson.IsObjectIdHex() method:
1
2
3
4
5
6
7
8
9
10
11
12
13
package db

import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

func IsObjectIDHex(hex string) bool {
_, err := primitive.ObjectIDFromHex(hex)
if err != nil {
return false
}
return true
}
  • Replace the bson.ObjectIdHex() method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package db

import (
"fmt"
"go.mongodb.org/mongo-driver/bson/primitive"
)

func ObjectIDHex(hex string) primitive.ObjectID {
id, err := primitive.ObjectIDFromHex(hex)
if err != nil {
panic(fmt.Sprintf("Invalid input to ObjectIDHex: %+v", hex))
}
return id
}

These functions can be placed in the package of the auxiliary method or the package of the database operation, and then replace the references in batches.

Summary

At this point, the project completed the migration within two hours and started running smoothly.
Thanks to the author of qmgo for making the wheels for me.
If I have the opportunity, I will also open up some wheels.

Read More
post @ 2020-12-21
  1. Human memory is very unreliable and easy to forget. Only by recording it permanently in words can you keep your memory for a longer time.
  2. There are also many good content on the Internet that are worth collecting, but other people’s blogs are not necessarily stable and reliable. It is possible that they are still there today and will be closed after a few days. I have to rely on myself to record reliably. After the knowledge is summarized and sorted out by myself, I also understand it more deeply.
  3. I hope that I will live forever, but this is not realistic. I hope this is an “immortal” blog that will perpetuate my knowledge and memory.
  4. This is not my first blog, and maybe it is not the last. There may be different ideas at every stage of life, and not all of these ideas are suitable for sharing.
  5. This blog is purely a technical blog, recording technical content. Ideas may change, technology is immortal, and truth is immortal.
Read More
⬆︎TOP