mongos

package
v1.1.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package mongos author gmfan date 2023/2/28

Package mongos author gmfan date 2023/3/2

Package mongos author gmfan date 2023/2/27

Index

Constants

View Source
const (
	MODEL1 = 1
	MODEL2 = 2
	MODEL3 = 3
)
View Source
const (
	DuplicateKeyCode = 11000
)

Variables

This section is empty.

Functions

func AutoIncID added in v1.1.7

func AutoIncID(ctx context.Context, table string, key string) (int64, error)

AutoIncID 获取自增 ID,key 为自增键。

func Bucket

func Bucket(table string) (bucket *gridfs.Bucket, err error)

func DB

func DB() *mongo.Database

func DelMany

func DelMany(ctx context.Context, table string, filter bson.D, deletedBy int64) (err error)

DelMany 根据 filter 软删除数据

func DownloadFile

func DownloadFile(table, fid string) (data []byte, err error)

DownloadFile 下载文件

func InitDatabase

func InitDatabase(ctx context.Context, c Conf) (err error)

InitDatabase 初始化 Mongo

func InsertMany added in v1.0.6

func InsertMany(ctx context.Context, table string, documents []any, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)

func InsertOne added in v1.0.6

func InsertOne(ctx context.Context, table string, document any, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)

func IsDuplicateKeyErr added in v1.1.7

func IsDuplicateKeyErr(err error) bool

func LoadEnvConf

func LoadEnvConf(c *Conf, must bool)

LoadEnvConf 加载环境变量中的 Mongo 配置,must 设置为环境变量中是否必须存在

func PageQuery

func PageQuery(ctx context.Context, table string, filter bson.D, page *model.Page, res any, projections ...string) (total int64, err error)

PageQuery 分页查询

func Query

func Query(ctx context.Context, table string, filter bson.D, res any, projections ...string) error

Query 简化多文档查询

func QueryOne

func QueryOne(ctx context.Context, table string, filter bson.D, res any, projections ...string) error

QueryOne 简化查询集合单条信息函数

func StructureBsonD

func StructureBsonD(arg any, model int) bson.D

StructureBsonD 构建bson D对象同时解析含_id后缀的值为ObjectID),MODEL1 直接构建不做处理 MODEL2 跳过默认值构建,MODEL3跳过默认值其中整型的默认值为-1

func ToObjectIDS

func ToObjectIDS(strs []string) (ids []primitive.ObjectID)

ToObjectIDS 将十六进制字符串类型ID转换为primitive.ObjectID

func UpdateMany added in v1.0.6

func UpdateMany(ctx context.Context, table string, filter bson.D, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func UpdateOne added in v1.0.6

func UpdateOne(ctx context.Context, table string, filter bson.D, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func UploadFile

func UploadFile(table string, data []byte) (fid string, err error)

UploadFile 上传文件,上传成功返回 fid

func UpsertMany

func UpsertMany(ctx context.Context, table string, filters []bson.D,
	updates any, upsert bool) (res *mongo.BulkWriteResult, err error)

UpsertMany 批量更新,filters与updates必须一致且filters与updates中元素一一对应

func UpsertOne

func UpsertOne(ctx context.Context, table string, filter bson.D, update any, upsert bool) (*mongo.UpdateResult, error)

UpsertOne 如果upsert=true则有则修改,无则新增。如果 update 类型不是 bson.D 则会被设置为 bson.D{{"$set",update}}

Types

type BaseModel

type BaseModel struct {
	ID        primitive.ObjectID `bson:"_id,omitempty" json:"id"`
	CreatedBy int64              `bson:"created_by,omitempty" json:"createdBy"`
	CreatedAt int64              `bson:"created_at,omitempty" json:"createdAt"`
	UpdatedBy int64              `bson:"updated_by,omitempty" json:"updatedBy"`
	UpdatedAt int64              `bson:"updated_at,omitempty" json:"updatedAt"`
	IsDeleted int8               `bson:"is_deleted" json:"isDeleted"`
}

func (*BaseModel) CreateByUID added in v1.0.4

func (b *BaseModel) CreateByUID(uid int64)

func (*BaseModel) UpdateByUID added in v1.0.4

func (b *BaseModel) UpdateByUID(uid int64)

type Conf

type Conf struct {
	URI         string
	DB          string
	MaxPoolSize int64
	// 单位秒
	MaxTimeout int64
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL