代码拉取完成,页面将自动刷新
package helper
import (
"context"
"errors"
"reflect"
"time"
"gitee.com/ktcatv/mgohelper"
"gitee.com/ktcatv/mgohelper/session"
"github.com/go-chassis/openlog"
"go.mongodb.org/mongo-driver/mongo"
)
type Service struct {
timeout time.Duration
}
func (r *Service) Aggregate(ctx context.Context, collname string, pipeline []map[string]interface{}, result interface{}) error {
cur, err := aggreagte(ctx, collname, pipeline)
if err != nil {
return err
}
if err = cur.All(ctx, result); err != nil {
return err
}
return nil
}
func (r *Service) CountDocuments(ctx context.Context, collname string, filter map[string]interface{}) (int64, error) {
return countDocuments(ctx, collname, filter)
}
func (r *Service) CreateOne(ctx context.Context, collname string, rec interface{}) error {
var err error
ctx, _ = context.WithTimeout(ctx, session.Timeout)
err = createOne(ctx, collname, rec)
return err
}
func (r *Service) CreateMany(ctx context.Context, collname string, recs ...interface{}) error {
var err error
ctx, _ = context.WithTimeout(ctx, session.Timeout)
err = createMany(ctx, collname, recs...)
return err
}
func (r *Service) FindOne(ctx context.Context, collname string, value interface{}, options ...mgohelper.FindOption) error {
ctx, _ = context.WithTimeout(context.Background(), session.Timeout)
opts := mgohelper.NewDefaultFindOpts()
for _, o := range options {
o(&opts)
}
result := findOne(ctx, collname, opts)
if err := result.Err(); err != nil {
// 查无记录时不返回错误
if err == mongo.ErrNoDocuments {
return nil
}
return err
}
return result.Decode(value)
}
func (r *Service) FindOneAndDelete(ctx context.Context, collname string, filter map[string]interface{}, value interface{}, options ...mgohelper.FindOption) error {
ctx, _ = context.WithTimeout(context.Background(), session.Timeout)
opts := mgohelper.NewDefaultFindOpts()
for _, o := range options {
o(&opts)
}
result := findOneAndDelete(ctx, collname, filter, opts)
_ = result.Decode(value)
if result.Err() != nil {
return result.Err()
}
return nil
}
func (r *Service) List(ctx context.Context, collname string, recs interface{}, options ...mgohelper.FindOption) (*mgohelper.Page, error) {
var typ reflect.Type
if typ = reflect.TypeOf(recs); typ.Kind() != reflect.Ptr {
return nil, errors.New("recs must be a Ptr")
}
if typ = typ.Elem(); typ.Kind() != reflect.Slice {
return nil, errors.New("recs data must be a slice")
}
slice := reflect.MakeSlice(typ, 0, 0)
if typ = typ.Elem(); typ.Kind() != reflect.Struct {
return nil, errors.New("recs element be a struct")
}
opts := mgohelper.NewDefaultFindOpts()
for _, o := range options {
o(&opts)
}
cur, total, err := find(ctx, collname, opts)
if err != nil {
return nil, err
}
defer cur.Close(ctx)
result := &mgohelper.Page{Total: total}
var count int
for cur.Next(ctx) {
rec := reflect.New(typ)
if err := cur.Decode(rec.Interface()); err != nil {
var vo reflect.Value = reflect.ValueOf(rec)
openlog.Error("decode to " + vo.Type().Name() + " error: " + err.Error())
return nil, err
}
slice = reflect.Append(slice, rec.Elem())
count++
}
v := reflect.ValueOf(recs).Elem()
v.Set(slice)
// result.List = recs
result.CorrectPagination(opts.Offset, opts.Limit, count)
return result, nil
}
func (r *Service) Pull(ctx context.Context, collname string, filter map[string]interface{}, value interface{}) error {
ct, _ := context.WithTimeout(ctx, session.Timeout)
_, err := pull(ct, collname, filter, value)
if err != nil {
return err
}
return nil
}
func (r *Service) RemoveMany(ctx context.Context, collname string, filter map[string]interface{}) error {
ct, _ := context.WithTimeout(ctx, session.Timeout)
_, err := removeMany(ct, collname, filter)
if err != nil {
return err
}
return nil
}
func (r *Service) Unmark(ctx context.Context, collname string, filter map[string]interface{}, value interface{}) error {
ct, _ := context.WithTimeout(ctx, session.Timeout)
_, err := unmark(ct, collname, filter, value)
if err != nil {
return err
}
return nil
}
func (r *Service) UpdateMany(ctx context.Context, collname string, filter interface{}, value interface{}) (*mongo.UpdateResult, error) {
return session.GetDB().Collection(collname).UpdateMany(ctx, filter, value)
}
func (r *Service) UpdateOne(ctx context.Context, collname string, filter map[string]interface{}, value interface{}, options ...mgohelper.FindOption) error {
ctx, _ = context.WithTimeout(context.Background(), session.Timeout)
opts := mgohelper.NewDefaultFindOpts()
for _, o := range options {
o(&opts)
}
_, err := updateOne(ctx, collname, filter, value)
if err != nil {
return err
}
return nil
}
// UpdateOneMore 允许在外部使用 $set 之外的操作符
func (r *Service) UpdateOneMore(ctx context.Context, collname string, filter map[string]interface{}, value interface{}, options ...mgohelper.FindOption) error {
ctx, _ = context.WithTimeout(context.Background(), session.Timeout)
opts := mgohelper.NewDefaultFindOpts()
for _, o := range options {
o(&opts)
}
_, err := updateOneMore(ctx, collname, filter, value)
if err != nil {
return err
}
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。