Qmgo
由于 mgo
这个库已经停止维护多年了,对新的特性支持较少,而 MongoDB driver
又不怎么好用,所以我发现了七牛云开源的 Qmgo
。
Qmgo
是一款Go
语言的MongoDB
driver
,它基于MongoDB 官方 driver 开发实现,同时使用更易用的接口设计,比如参考mgo (比如mgo
的链式调用)。
Qmgo
让您以更优雅的姿势使用MongoDB
的新特性。Qmgo
是从mgo
迁移到新MongoDB driver
的第一选择,对代码的改动影响最小。
项目地址:https://github.com/qiniu/qmgo
本文仅演示简单的CRUD操作,其他操作可参阅其文档
CRUD
开始
第一步当然是安装依赖
go get github.com/qiniu/qmgo
连接数据库和选择数据库和表
这里有两种方式:
ctx := context.Background()
client, err := qmgo.NewClient(ctx, &qmgo.Config{Uri: "mongodb://localhost:27017"})
db := client.Database("kakkk_test")
coll := db.Collection("user")
cli, err := qmgo.Open(ctx, &qmgo.Config{Uri: "mongodb://localhost:27017", Database: "kakkk_test", Coll: "user"})
定义本文使用的结构体:
type User struct {
UserName string `bson:"user_name"`
Password string `bson:"password"`
Age uint16 `bson:"age"`
}
文档用的是后者举例,本文使用前者进行举例(一样的)
插入
插入一条
result, err := coll.InsertOne(ctx, User{"user1", "pwd123", 18})
插入多条
var users = []User{
{"user2", "pwd123", 19},
{"user3", "pwd123", 20},
{"user4", "pwd123", 21},
{"user5", "pwd123", 22},
}
result, err := coll.InsertMany(ctx, users)
查找
查找一个
one := &User{}
err := coll.Find(ctx, bson.M{"user_name": "user1"}).One(&one)
查找多个
var many []User
err := coll.Find(ctx, bson.M{"age": bson.M{"$gte": 20, "$lte": 21}}).All(&many)
删除
err := coll.Remove(ctx, bson.M{"user_name": "user1"})
修改
修改一个
err := coll.UpdateOne(ctx, bson.M{"user_name": "user2"}, bson.M{"$set": bson.M{"age": 30}})
修改多个
result, err := coll.UpdateAll(ctx, bson.M{"age": bson.M{"$gte": 20, "$lte": 21}}, bson.M{"$set": bson.M{"age": 30}})
完整示例代码
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/qiniu/qmgo"
"go.mongodb.org/mongo-driver/bson"
)
type User struct {
UserName string `bson:"user_name"`
Password string `bson:"password"`
Age uint16 `bson:"age"`
}
func (u *User) String() string {
b, err := json.Marshal(*u)
if err != nil {
return fmt.Sprintf("%+v", *u)
}
var out bytes.Buffer
err = json.Indent(&out, b, "", " ")
if err != nil {
return fmt.Sprintf("%+v", *u)
}
return out.String()
}
func main() {
ctx := context.Background()
//连接数据库
client, err := qmgo.NewClient(ctx, &qmgo.Config{Uri: "mongodb://localhost:27017"})
if err != nil {
return
}
//选择数据库
db := client.Database("kakkk_test")
//选择collection
coll := db.Collection("user")
//插入
//插入一个
_, _ = coll.InsertOne(ctx, User{"user1", "pwd123", 18})
//插入多个
var users = []User{
{"user2", "pwd123", 19},
{"user3", "pwd123", 20},
{"user4", "pwd123", 21},
{"user5", "pwd123", 22},
}
_, _ = coll.InsertMany(ctx, users)
//查找
//查找一个
one := &User{}
_ = coll.Find(ctx, bson.M{"user_name": "user1"}).One(&one)
fmt.Println(one.String())
//查找多个
var many []User
_ = coll.Find(ctx, bson.M{"age": bson.M{"$gte": 20, "$lte": 21}}).All(&many)
for _, user := range many {
fmt.Println(user.String())
}
//删除
_ = coll.Remove(ctx, bson.M{"user_name": "user1"})
user1 := &User{}
_ = coll.Find(ctx, bson.M{"user_name": "user1"}).One(&user1)
fmt.Println(user1.String())
//修改
//修改一个
_ = coll.UpdateOne(ctx, bson.M{"user_name": "user2"}, bson.M{"$set": bson.M{"age": 30}})
user2 := &User{}
_ = coll.Find(ctx, bson.M{"user_name": "user2"}).One(&user2)
fmt.Println(user2.String())
//修改多个
_, _ = coll.UpdateAll(ctx, bson.M{"age": bson.M{"$gte": 20, "$lte": 21}}, bson.M{"$set": bson.M{"age": 30}})
var usersUpdated []User
_ = coll.Find(ctx, bson.M{"age": bson.M{"$gte": 20, "$lte": 21}}).All(&usersUpdated)
for _, user := range usersUpdated {
fmt.Println(user.String())
}
}