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