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
- func AutoIncID(ctx context.Context, table string, key string) (int64, error)
- func Bucket(table string) (bucket *gridfs.Bucket, err error)
- func DB() *mongo.Database
- func DelMany(ctx context.Context, table string, filter bson.D, deletedBy int64) (err error)
- func DownloadFile(table, fid string) (data []byte, err error)
- func InitDatabase(ctx context.Context, c Conf) (err error)
- func InsertMany(ctx context.Context, table string, documents []any, ...) (*mongo.InsertManyResult, error)
- func InsertOne(ctx context.Context, table string, document any, ...) (*mongo.InsertOneResult, error)
- func IsDuplicateKeyErr(err error) bool
- func LoadEnvConf(c *Conf, must bool)
- func PageQuery(ctx context.Context, table string, filter bson.D, page *model.Page, res any, ...) (total int64, err error)
- func Query(ctx context.Context, table string, filter bson.D, res any, ...) error
- func QueryOne(ctx context.Context, table string, filter bson.D, res any, ...) error
- func StructureBsonD(arg any, model int) bson.D
- func ToObjectIDS(strs []string) (ids []primitive.ObjectID)
- func UpdateMany(ctx context.Context, table string, filter bson.D, update any, ...) (*mongo.UpdateResult, error)
- func UpdateOne(ctx context.Context, table string, filter bson.D, update any, ...) (*mongo.UpdateResult, error)
- func UploadFile(table string, data []byte) (fid string, err error)
- func UpsertMany(ctx context.Context, table string, filters []bson.D, updates any, upsert bool) (res *mongo.BulkWriteResult, err error)
- func UpsertOne(ctx context.Context, table string, filter bson.D, update any, upsert bool) (*mongo.UpdateResult, error)
- type BaseModel
- type Conf
Constants ¶
View Source
const ( MODEL1 = 1 MODEL2 = 2 MODEL3 = 3 )
View Source
const (
DuplicateKeyCode = 11000
)
Variables ¶
This section is empty.
Functions ¶
func InitDatabase ¶
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 LoadEnvConf ¶
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 QueryOne ¶
func QueryOne(ctx context.Context, table string, filter bson.D, res any, projections ...string) error
QueryOne 简化查询集合单条信息函数
func StructureBsonD ¶
StructureBsonD 构建bson D对象同时解析含_id后缀的值为ObjectID),MODEL1 直接构建不做处理 MODEL2 跳过默认值构建,MODEL3跳过默认值其中整型的默认值为-1
func ToObjectIDS ¶
ToObjectIDS 将十六进制字符串类型ID转换为primitive.ObjectID
func UpdateMany ¶ added in v1.0.6
func UploadFile ¶
UploadFile 上传文件,上传成功返回 fid
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 (*BaseModel) UpdateByUID ¶ added in v1.0.4
Source Files ¶
Click to show internal directories.
Click to hide internal directories.