初始化
This commit is contained in:
96
mongo/mongo.go
Normal file
96
mongo/mongo.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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"
|
||||
)
|
||||
|
||||
var db = new(mongo.Database)
|
||||
|
||||
func init() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
link, _ := g.Cfg().Get(context.Background(), "mongo.address")
|
||||
mongoAddr := link.String()
|
||||
client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoAddr))
|
||||
if err != nil {
|
||||
glog.Error(ctx, "mongodb连接失败")
|
||||
}
|
||||
dbName := gstr.SubStr(mongoAddr, strings.LastIndex(mongoAddr, "/")+1, len(mongoAddr))
|
||||
db = client.Database(dbName)
|
||||
}
|
||||
|
||||
// Find 查询多条记录
|
||||
func Find(ctx context.Context, filter *primitive.M, result interface{}, collection string, opts ...*options.FindOptions) (err error) {
|
||||
if err = utils.ValidStructPtr(result); err != nil {
|
||||
return
|
||||
}
|
||||
cur, err := db.Collection(collection).Find(ctx, filter, opts...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = cur.All(ctx, result)
|
||||
return
|
||||
}
|
||||
|
||||
// FindOne 查询1条记录
|
||||
func FindOne(ctx context.Context, filter *primitive.M, result interface{}, collection string, opts ...*options.FindOneOptions) (err error) {
|
||||
if len(*filter) == 0 {
|
||||
err = gerror.New("缺少查询条件")
|
||||
return
|
||||
}
|
||||
if err = utils.ValidStructPtr(result); err != nil {
|
||||
return
|
||||
}
|
||||
cur := db.Collection(collection).FindOne(ctx, filter, opts...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = cur.Decode(result)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除记录
|
||||
func Delete(ctx context.Context, filter *primitive.M, collection string, opts ...*options.DeleteOptions) (count int64, err error) {
|
||||
r, err := db.Collection(collection).DeleteMany(ctx, filter, opts...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
count = r.DeletedCount
|
||||
return
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
result, err = db.Collection(collection).UpdateMany(ctx, filter, update, opts...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
ids = r.InsertedIDs
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user