社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
最近,七牛 CDN 研发团队开源了Qmgo[1] - Go 语言的 MongoDB driver,它基于Mongo 官方 driver[2]实现,但是有着更好的易用性,设计上参考了老牌的 driver Mgo[3] (比如 Mgo 的链式调用)。
做 Qmgo 的初衷,来自于使用 MongoDB 的 gopher 们共同的困扰,在 MongoDB 的 Go 官方 driver 成型前(v1.0.0 发布在 2019 年 3 月),一直是 Mgo 一统江湖,出色的接口设计让其非常流行。七牛做为最早使用 Go 的公司之一,自然也是 Mgo 的深度用户。
但是 Mgo 已经在 3 年前不再维护,bug 不修复,MongoDB 的新特性自然也无法支持,而官方 driver 的接口设计是出名的不易用。
这样的背景下,基于满足下面的需求,Qmgo 诞生了
下面,简单介绍一下 Qmgo 的特点,详情可以点击查看:Qmgo[4]
举一个多文件查找、sort
和limit
的例子, 说明qmgo
和mgo
的相似,以及对go.mongodb.org/mongo-driver
的改进
官方Driver
需要这样实现
// 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 := coll.Find(ctx, bson.M{"age": 6}, findOptions)
cur.All(ctx, &batch)
Qmgo
和mgo
更简单,而且实现相似:
// 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)
Sort
、Limit
、Count
、Select
Cursor
Aggregate
import
并新建连接import(
"context"
"github.com/qiniu/qmgo"
)
ctx := context.Background()
client, err := qmgo.NewClient(ctx, &qmgo.Config{Uri: "mongodb://localhost:27017"})
db := client.Database("class")
coll := db.Collection("user")
如果你的连接是指向固定的 database 和 collection,我们推荐使用下面的更方便的方法初始化连接,后续操作都基于cli
而不用再关心 database 和 collection
cli, err := qmgo.Open(ctx, &qmgo.Config{Uri: "mongodb://localhost:27017", Database: "class", Coll: "user"})
*后面都会基于cli
来举例,如果你使用第一种传统的方式进行初始化,根据上下文,将cli
替换成client
、db
或 coll
即可*
在初始化成功后,请defer
来关闭连接
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,
}
创建索引
cli.EnsureIndexes(ctx, []string{}, []string{"age", "name,weight"})
// insert one document
result, err := cli.Insert(ctx, oneUserInfo)
// find one document
one := UserInfo{}
err = cli.Find(ctx, bson.M{"name": oneUserInfo.Name}).One(&one)
err = cli.Remove(ctx, bson.M{"age": 7})
// 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)
Sort
和Limit
// find all 、sort and limit
batch := []UserInfo{}
cli.Find(ctx, bson.M{"age": 6}).Sort("weight").Limit(7).All(&batch)
count, err := cli.Find(ctx, bson.M{"age": 6}).Count()
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)
Qmgo: https://github.com/qiniu/qmgo
[2]Mongo官方driver: https://github.com/mongodb/mongo-go-driver
[3]Mgo: https://github.com/go-mgo/mgo
[4]Qmgo: https://github.com/qiniu/qmgo
推荐阅读
Go 每日一库之一个有趣的分布式缓存实现 — groupcache
站长 polarisxu
自己的原创文章
不限于 Go 技术
职场和创业经验
Go语言中文网
每天为你
分享 Go 知识
Go爱好者值得关注
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!