Recently, Qmgo[1] -go MongoDB driver has been opened source by Qmgo[1] -Go R&D team. Once released, it has been reported by “CSDN”, “Go Language Chinese” and other media and technology developers widely concerned. Qmgo refers to the old driver Mgo[3] in design (such as chain call of Mgo), which is based on Mongo official driver[2], but has better usability. Let’s take a closer look at this open source project. Background The original intention of Qmgo comes from the common trouble of gopher who use MongoDB. Before the official MongoDB Go driver took shape (V1.0.0 was released in March 2019), Mgo had been dominating the arena. Excellent interface design makes it very popular. As one of the earliest companies to use Go, Qiniu is also a deep user of Mgo.

However, Mgo was no longer maintained three years ago, bugs are not fixed, new MongoDB features are not supported, and the interface design of the official driver is notoriously difficult to use.

In this context, Qmgo was born based on meeting the following requirements

Want new features of MongoDB, want more stable driver, want excellent interface design of Mgo, want to migrate from Mgo to Qmgo, code changes are minimal below, briefly introduce the characteristics of Qmgo, details can be viewed: Qmgo[4]

Where is it useful? Use an example of multi-file lookup, sort, and limit to illustrate qmgo’s similarities to MGO and improvements to go.mongodb.org/mongo-driver

The official Driver needs to do this

// go.mongodb.org/mongo-driver // find all, sort and limit findOptions := options.find () findoptions.setlimit (7) // set limit var sorts D sorts = append(sorts, E{Key: “weight”, Value: 1}) findOptions.SetSort(sorts) // set sort

Batch := []UserInfo{} cur, err := Col.find (CTX, bson.M{“age”: 6}, findOptions) cur.All(CTX, & Batch) Qmgo and mGO are more simple and implement similar:

// qmgo // find all, sort and limit Batch := []UserInfo{} cli. find (CTX, bson.M{“age”: 6}).Sort(“weight”).Limit(7).All(&batch)

// mgo // find all, sort and limit coll. find (bson.M{“age”: 6}).sort (“weight”).limit (7).all (& Batch) Add, delete, change, query index configuration Sort, Limit, Count, Select Cursor Aggregate Aggregate Start, Import and create a new connection import(“context”

"github.com/qiniu/qmgo"
Copy the code

) ctx := context.Background() client, err := qmgo.NewClient(ctx, &qmgo.Config{Uri: “Mongo: / / localhost: 27017″}) db: = client. The Database (” class “) coll: = the Collection (” user “) if your connection is pointing to the Database and fixed For collection, we recommend the following more convenient method to initialize the connection, and all subsequent operations are based on the CLI without worrying about database and collection

cli, err := qmgo.Open(ctx, &qmgo.Config{Uri: “mongodb://localhost:27017”, Database: “class”, Coll: “User “}) are based on the CLI. If you use the first traditional way of initialization, replace the CLI with client, DB, or COLl, depending on the context

After successful initialization, please defer to close the connection

defer func() { if err = cli.Close(ctx); err ! = nil {panic(err)}}();

type UserInfo struct {

Name string bson:"name"

Age uint16 bson:"age"

Weight uint32 bson:"weight"

}

Var oneUserInfo = UserInfo{Name: “xm”, Age: 7, Weight: 40,} Create an index

Cli.ensureindexes (CTX, []string{}, [] String {“age”, “name,weight”}) Err := cli.Insert(CTX, oneUserInfo) to find a document // find one document one := UserInfo{} err = cli. find (CTX, bson.M{“name”: Oneuserinfo.name}).One(& One) delete document err = cli.remove (CTX, bson.M{“age”: // Multiple insert var batchUserInfoI = []interface{}{UserInfo{Name: “a1”, Age: 6, Weight: 20}, UserInfo{Name: “b2”, Age: 6, Weight: 25}, UserInfo{Name: “c3”, Age: 6, Weight: 30}, UserInfo{Name: “d4”, Age: 6, Weight: 35}, UserInfo{Name: “a1”, Age: 7, Weight: 40}, UserInfo{Name: “a1”, Age: 8, Weight: 45}, } result, err = cli.Collection.InsertMany(ctx, BatchUserInfoI) batch search, Sort and Limit // find all, Sort and Limit Batch := []UserInfo{} cli. find (CTX, bson.M{“age”: 6}).Sort(“weight”).Limit(7).All(&batch) Count count, err := cli.Find(ctx, bson.M{“age”: 6}).Count() Update // UpdateOne one err := cli.UpdateOne(ctx, bson.M{“name”: “d4”}, bson.M{“$set”: bson.M{“age”: 7}})

// UpdateAll

result, err := cli.UpdateAll(ctx, bson.M{“age”: 6}, bson.M{“set”: bson.M{“age”: 10}}) Select err := cli.Find(ctx, bson.M{“age”: 10}).Select(bson.M{“age”: 1}).One(&one) Aggregate matchStage := bson.D{{“match”, []bson.E{{“weight”, bson.D{{“gt”, 30}}}}}} groupStage := bson.D{{“group”, bson.D{{“_id”, “name”}, {“total”, bson.D{{“sum”, “$age”}}}}}}

var showsWithInfo []bson.M

err = cli.Aggregate(context.Background(), Pipeline{matchStage, groupStage}).All(&showsWithInfo)

Reference [1] Qmgo: github.com/qiniu/qmgo

[2] Mongo official driver: github.com/mongodb/mon…

[3]

Mgo: github.com/go-mgo/mgo

[4]

Qmgo: github.com/qiniu/qmgo