go-mongox 基于 泛型 对 MongoDB 官方框架进行了二次封装,它通过使用链式调用的方式,让我们能够丝滑地操作文档。同时,其还提供了多种类型的 bson 构造器,帮助我们高效的构建 bson 数据。
功能持续更新和改进中,对该框架感兴趣的伙伴,欢迎提出宝贵的意见和参与贡献。
最新详细教程 → go-mongox:简单高效,让文档操作和 bson 数据构造更流畅
go-mongox 框架有两个核心,一个核心是基于泛型的 collection 形态,另一个核心是 builder 构造器。
go get github.com/chenmingyong0423/go-mongox@latest
创建一个基于泛型类型 Pot 的 Collection 实例。
// 示例代码,不是最佳的创建方式
func newCollection() *mongo.Collection {
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://localhost:27017").SetAuth(options.Credential{
Username: "test",
Password: "test",
AuthSource: "db-test",
}))
if err != nil {
panic(err)
}
err = client.Ping(context.Background(), readpref.Primary())
if err != nil {
panic(err)
}
collection := client.Database("db-test").Collection("test_post")
return collection
}
type Post struct {
Id string `bson:"_id"`
Title string `bson:"title"`
Author string `bson:"author"`
Content string `bson:"content"`
}
// 你需要预先创建一个 *mongo.Collection 对象
mongoCollection := newCollection()
// 使用 Post 结构体作为泛型参数创建一个 collection
postCollection := mongox.NewCollection[Post](mongoCollection)
Creator 是一个创造器,用于执行插入相关的操作。
通过 Finder() 方法创建一个查询器,基于该查询器我们可以进行相关的查询操作。设置查询条件的方法是Filter,设置 *options.FindOneOptions 参数的方法有:OneOptions 和 Options。
如果查询条件 filter 非常简单,我们可以通过 bsonx.M 方法进行构造。如果 filter 过于复杂,我们可以使用 query 包里的 BsonBuilder() 构造器进行构造。
Updater
Updater 是一个更新器,用于执行更新相关的操作。
通过 Updater() 方法创建一个更新器,基于该更新器我们可以进行相关的更新操作。设置文档匹配条件的方法是 Filter,设置更新内容的方法有:Updates 和 UpdatesWithOperator,设置 *options.UpdateOptions 参数的方法有:Options。
其中 UpdatesWithOperator 方法需要我们指定操作符。
Deleter 是一个删除器,用于执行删除相关的操作。
// 删除单个文档
deleteResult, err := postCollection.Deleter().Filter(bsonx.Id("1")).DeleteOne(context.Background())
// 携带 option 参数
deleteResult, err := postCollection.Deleter().Filter(bsonx.Id("1")).Options(options.Delete().SetComment("test")).DeleteOne(context.Background())
// 删除多个文档
// - 通过 query 包构造复杂的 bson 语句
deleteResult, err = postCollection.Deleter().Filter(query.BsonBuilder().InString("_id", []string{"2", "3", "4"}...).Build()).DeleteMany(context.Background())
通过 Deleter() 方法创建一个删除器,基于该删除器我们可以进行相关的删除操作。设置文档匹配条件的方法有:Filter,设置 *options.DeleteOptions 参数的方法有:Options。
Aggregator
Aggregator 是一个聚合器,用于执行聚合相关的操作。
// 聚合操作
// - 使用 aggregation 包构造 pipeline
posts, err = postCollection.
Aggregator().Pipeline(aggregation.StageBsonBuilder().Project(bsonx.M("content", 0)).Build()).
Aggregate(context.Background())
// 如果我们通过聚合操作更改字段的名称,那么我们可以使用 AggregationWithCallback 方法,然后通过 callback 函数将结果映射到我们预期的结构体中
type DiffPost struct {
Id string `bson:"_id"`
Title string `bson:"title"`
Name string `bson:"name"` // author → name
Content string `bson:"content"`
Outstanding bool `bson:"outstanding"`
}
result := make([]*DiffPost, 0)
// 将 author 字段更名为 name,排除 content 字段,添加 outstanding 字段,返回结果为 []*DiffPost
err = postCollection.Aggregator().
Pipeline(aggregation.StageBsonBuilder().Project(
bsonx.NewD().
Add("name", "$author").
Add("author", 1).
Add("_id", 1).
Add("title", 1).
Add("outstanding", aggregation.BsonBuilder().Eq("$author", "陈明勇").Build()).Build(),
).Build(),
).
AggregateWithCallback(context.Background(), func(ctx context.Context, cursor *mongo.Cursor) error {
return cursor.All(ctx, &result)
})
https://github.com/chenmingyong0423/go-mongox
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。