mongo工具类修改

This commit is contained in:
2025-11-25 14:19:52 +08:00
parent 718d6a2528
commit 8665705f26

View File

@@ -37,7 +37,7 @@ func init() {
}
// Find 查询多条记录
func Find(ctx context.Context, filter bson.M, result interface{}, collection string, opts ...options.FindOptions) (err error) {
func Find(ctx context.Context, filter bson.M, result interface{}, collection string, opts ...options.Lister[options.FindOptions]) (err error) {
if err = utils.ValidStructPtr(result); err != nil {
return
}
@@ -50,7 +50,7 @@ func Find(ctx context.Context, filter bson.M, result interface{}, collection str
}
// FindOne 查询1条记录
func FindOne(ctx context.Context, filter bson.M, result interface{}, collection string, opts ...options.FindOneOptions) (err error) {
func FindOne(ctx context.Context, filter bson.M, result interface{}, collection string, opts ...options.Lister[options.FindOneOptions]) (err error) {
if len(filter) == 0 {
err = gerror.New("缺少查询条件")
return
@@ -67,7 +67,7 @@ func FindOne(ctx context.Context, filter bson.M, result interface{}, collection
}
// Delete 删除记录
func Delete(ctx context.Context, filter bson.M, collection string, opts ...options.DeleteOptions) (count int64, err error) {
func Delete(ctx context.Context, filter bson.M, collection string, opts ...options.Lister[options.DeleteManyOptions]) (count int64, err error) {
r, err := db.Collection(collection).DeleteMany(ctx, filter, opts...)
if err != nil {
return
@@ -77,7 +77,7 @@ func Delete(ctx context.Context, filter bson.M, collection string, opts ...optio
}
// Update 修改记录
func Update(ctx context.Context, filter bson.M, update interface{}, collection string, opts ...options.UpdateOptions) (result *mongo.UpdateResult, err error) {
func Update(ctx context.Context, filter bson.M, update interface{}, collection string, opts ...options.Lister[options.UpdateManyOptions]) (result *mongo.UpdateResult, err error) {
result, err = db.Collection(collection).UpdateMany(ctx, filter, update, opts...)
if err != nil {
return
@@ -86,7 +86,7 @@ func Update(ctx context.Context, filter bson.M, update interface{}, collection s
}
// 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.Lister[options.InsertManyOptions]) (ids []interface{}, err error) {
r, err := db.Collection(collection).InsertMany(ctx, documents, opts...)
if err != nil {
return