修改为Mongo Driver V2写法
This commit is contained in:
@@ -2,17 +2,17 @@ package mongo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitee.com/red-future---jilin-g/common/utils"
|
"gitee.com/red-future---jilin-g/common/utils"
|
||||||
"github.com/gogf/gf/v2/errors/gerror"
|
"github.com/gogf/gf/v2/errors/gerror"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/os/glog"
|
"github.com/gogf/gf/v2/os/glog"
|
||||||
"github.com/gogf/gf/v2/text/gstr"
|
"github.com/gogf/gf/v2/text/gstr"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
"strings"
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||||
"time"
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var db = new(mongo.Database)
|
var db = new(mongo.Database)
|
||||||
@@ -22,16 +22,22 @@ func init() {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
link, _ := g.Cfg().Get(context.Background(), "mongo.address")
|
link, _ := g.Cfg().Get(context.Background(), "mongo.address")
|
||||||
mongoAddr := link.String()
|
mongoAddr := link.String()
|
||||||
client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoAddr))
|
opt := options.Client().ApplyURI(mongoAddr)
|
||||||
|
client, err := mongo.Connect(opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Error(ctx, "mongodb连接失败")
|
glog.Error(ctx, "mongodb连接失败", err)
|
||||||
}
|
}
|
||||||
|
// 从连接串中解析数据库名
|
||||||
dbName := gstr.SubStr(mongoAddr, strings.LastIndex(mongoAddr, "/")+1, len(mongoAddr))
|
dbName := gstr.SubStr(mongoAddr, strings.LastIndex(mongoAddr, "/")+1, len(mongoAddr))
|
||||||
|
// 如果连接串带有参数(如 ?retryWrites=true),需要去掉参数部分
|
||||||
|
if strings.Contains(dbName, "?") {
|
||||||
|
dbName = gstr.SubStr(dbName, 0, strings.Index(dbName, "?"))
|
||||||
|
}
|
||||||
db = client.Database(dbName)
|
db = client.Database(dbName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find 查询多条记录
|
// Find 查询多条记录
|
||||||
func Find(ctx context.Context, filter *primitive.M, result interface{}, collection string, opts ...*options.FindOptions) (err error) {
|
func Find(ctx context.Context, filter bson.M, result interface{}, collection string, opts ...options.FindOptions) (err error) {
|
||||||
if err = utils.ValidStructPtr(result); err != nil {
|
if err = utils.ValidStructPtr(result); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -44,8 +50,8 @@ func Find(ctx context.Context, filter *primitive.M, result interface{}, collecti
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindOne 查询1条记录
|
// FindOne 查询1条记录
|
||||||
func FindOne(ctx context.Context, filter *primitive.M, result interface{}, collection string, opts ...*options.FindOneOptions) (err error) {
|
func FindOne(ctx context.Context, filter bson.M, result interface{}, collection string, opts ...options.FindOneOptions) (err error) {
|
||||||
if len(*filter) == 0 {
|
if len(filter) == 0 {
|
||||||
err = gerror.New("缺少查询条件")
|
err = gerror.New("缺少查询条件")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -53,9 +59,6 @@ func FindOne(ctx context.Context, filter *primitive.M, result interface{}, colle
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
cur := db.Collection(collection).FindOne(ctx, filter, opts...)
|
cur := db.Collection(collection).FindOne(ctx, filter, opts...)
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = cur.Decode(result)
|
err = cur.Decode(result)
|
||||||
if err == mongo.ErrNoDocuments {
|
if err == mongo.ErrNoDocuments {
|
||||||
err = nil
|
err = nil
|
||||||
@@ -64,7 +67,7 @@ func FindOne(ctx context.Context, filter *primitive.M, result interface{}, colle
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Delete 删除记录
|
// Delete 删除记录
|
||||||
func Delete(ctx context.Context, filter *primitive.M, collection string, opts ...*options.DeleteOptions) (count int64, err error) {
|
func Delete(ctx context.Context, filter bson.M, collection string, opts ...options.DeleteOptions) (count int64, err error) {
|
||||||
r, err := db.Collection(collection).DeleteMany(ctx, filter, opts...)
|
r, err := db.Collection(collection).DeleteMany(ctx, filter, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -74,10 +77,7 @@ func Delete(ctx context.Context, filter *primitive.M, collection string, opts ..
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update 修改记录
|
// Update 修改记录
|
||||||
func Update(ctx context.Context, filter *primitive.M, update interface{}, result *mongo.UpdateResult, collection string, opts ...*options.UpdateOptions) (err error) {
|
func Update(ctx context.Context, filter bson.M, update interface{}, collection string, opts ...options.UpdateOptions) (result *mongo.UpdateResult, err error) {
|
||||||
if err = utils.ValidStructPtr(result); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
result, err = db.Collection(collection).UpdateMany(ctx, filter, update, opts...)
|
result, err = db.Collection(collection).UpdateMany(ctx, filter, update, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -85,8 +85,8 @@ func Update(ctx context.Context, filter *primitive.M, update interface{}, result
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert 修改记录
|
// Insert 插入多条记录
|
||||||
func Insert(ctx context.Context, documents []interface{}, collection string, opts ...*options.InsertManyOptions) (ids []interface{}, err error) {
|
func Insert(ctx context.Context, documents []interface{}, collection string, opts ...options.InsertManyOptions) (ids []interface{}, err error) {
|
||||||
r, err := db.Collection(collection).InsertMany(ctx, documents, opts...)
|
r, err := db.Collection(collection).InsertMany(ctx, documents, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user