Documentation ¶
Index ¶
- Variables
- func Aggregate(m IDefaultModel, pipeline any, result any) error
- func CloseAll() error
- func Col(name string) *qmgo.Collection
- func Ctx() context.Context
- func Delete(m IDefaultModel) error
- func Distinct(m IDefaultModel, filter any, field string, result any) error
- func ExistInDB(m IDefaultModel, filter any) bool
- func FindById(colName string, id string, res any) (bool, error)
- func FindOne(m IDefaultModel, filter map[string]any) (hasResult bool)
- func FindPage(m IDefaultModel, filter any, res any, pageSize int64, currentPage int64) (totalDoc int64, totalPage int64)
- func FindPageWithOption(m IDefaultModel, filter any, res any, pageSize int64, currentPage int64, ...) (totalDoc int64, totalPage int64)
- func InitWgm(connectionUri string, databaseName string) errordeprecated
- func Insert(m IDefaultModel) (*qmgo.InsertOneResult, error)
- func IsNoResult(err error) bool
- func MustHexToObjectId(strId string) primitive.ObjectID
- func MustUpdater(m any) *updater
- func NewWGM(connectionUri string, databaseName string) error
- func Ping() error
- func Update(m IDefaultModel, filter ...map[string]any) error
- func Updater(m any) (*updater, error)
- type DefaultModel
- func (m *DefaultModel) BeforeInsert(ctx context.Context) error
- func (m *DefaultModel) BeforeUpdate(ctx context.Context) error
- func (m *DefaultModel) BeforeUpsert(ctx context.Context) error
- func (m *DefaultModel) ColName() string
- func (m *DefaultModel) GetId() string
- func (m *DefaultModel) GetObjectID() primitive.ObjectID
- func (m *DefaultModel) PutId(id string)
- type FindPageOption
- type IDefaultModel
Constants ¶
This section is empty.
Variables ¶
var FindPageOptionSyncPool = sync.Pool{ New: func() interface{} { return new(FindPageOption) }, }
FindPageOptionSyncPool is a sync.Pool that stores FindPageOption objects.
Functions ¶
func Aggregate ¶
func Aggregate(m IDefaultModel, pipeline any, result any) error
Aggregate @param m: 查询合集 @param pipeline: 聚合管道,必须为数组 @param result: 查询结果,必须为指向数组的指针 @Description: 聚合查询,详情见 https://www.mongodb.com/docs/manual/aggregation/
func Col ¶
func Col(name string) *qmgo.Collection
func Delete ¶
func Delete(m IDefaultModel) error
func Distinct ¶
func Distinct(m IDefaultModel, filter any, field string, result any) error
Distinct @param m: 查询合集 @param filter: 查询前过滤doc @param field: 去重字段 @param resultSlice: 查询结果,必须为指向数组的指针 @Description: 去重查询,详情见 https://docs.mongodb.com/manual/reference/command/distinct/
func ExistInDB ¶
func ExistInDB(m IDefaultModel, filter any) bool
ExistInDB 查询是否存在数据库 m 查询的合集 filter 查询条件,查询全部文档使用 nil,查询条件使用 bson.M bool 是否存在
func FindOne ¶
func FindOne(m IDefaultModel, filter map[string]any) (hasResult bool)
FindOne 查询符合条件的第一条数据 m 查询的合集,结果也会被绑定在这 filter 查询条件,查询全部文档使用 nil,查询条件使用 bson.M hasResult 是否查询到结果
func FindPage ¶
func FindPage(m IDefaultModel, filter any, res any, pageSize int64, currentPage int64) (totalDoc int64, totalPage int64)
FindPage 数据库分页查询 m 查询的合集 filter 查询条件,查询全部文档使用 nil,查询条件使用 bson.M res 结果集指针,必须为指向切片的指针!!! pageSize 页面大小 currentPage 当前页面 totalDoc 总数据数量 totalPage 总页面数量
func FindPageWithOption ¶
func FindPageWithOption(m IDefaultModel, filter any, res any, pageSize int64, currentPage int64, option *FindPageOption) (totalDoc int64, totalPage int64)
FindPageWithOption 数据库多条件分页查询 m 查询的合集 filter 查询条件,查询全部文档使用 nil,查询条件使用 bson.M res 结果集指针,必须为指向切片的指针!!! pageSize 页面大小 currentPage 当前页面 totalDoc 总数据数量 totalPage 总页面数量
func Insert ¶
func Insert(m IDefaultModel) (*qmgo.InsertOneResult, error)
func IsNoResult ¶
IsNoResult 是否结果不存在 err 数据库查询后返回的 err bool 结果,true 为未查询到数据,反之亦然
func MustHexToObjectId ¶
func MustUpdater ¶ added in v1.0.5
func MustUpdater(m any) *updater
func NewWGM ¶ added in v1.0.5
NewWGM initializes the connection to the specified database using the provided connection URI and database name.
Types ¶
type DefaultModel ¶
type DefaultModel struct { Id primitive.ObjectID `bson:"_id" json:"id"` CreateTime int64 `bson:"create_time" json:"create_time"` LastModifyTime int64 `bson:"last_modify_time" json:"last_modify_time"` }
DefaultModel represents a default model for MongoDB documents.
func (*DefaultModel) BeforeInsert ¶
func (m *DefaultModel) BeforeInsert(ctx context.Context) error
BeforeInsert sets the default values for the Id, CreateTime, and LastModifyTime fields of the DefaultModel before inserting it into the database. It is called before inserting a new document. This method should be called within the context of a transaction. It does not return an error.
func (*DefaultModel) BeforeUpdate ¶
func (m *DefaultModel) BeforeUpdate(ctx context.Context) error
BeforeUpdate updates the last modify time of the DefaultModel instance to the current time.
func (*DefaultModel) BeforeUpsert ¶
func (m *DefaultModel) BeforeUpsert(ctx context.Context) error
BeforeUpsert sets defaults for Id, CreateTime, and LastModifyTime before upserting the DefaultModel.
func (*DefaultModel) ColName ¶
func (m *DefaultModel) ColName() string
ColName returns the name of the collection to which the DefaultModel belongs.
func (*DefaultModel) GetId ¶
func (m *DefaultModel) GetId() string
GetId returns the hexadecimal string representation of the Id field of the DefaultModel. It uses the Hex() method from the Id field to convert the Id to its string representation.
func (*DefaultModel) GetObjectID ¶
func (m *DefaultModel) GetObjectID() primitive.ObjectID
GetObjectID returns the ObjectID of the DefaultModel. The ObjectID is used to uniquely identify the DefaultModel in the database. It is retrieved from the `Id` field of the DefaultModel struct.
func (*DefaultModel) PutId ¶
func (m *DefaultModel) PutId(id string)
PutId updates the "Id" field of the DefaultModel with the specified value. The input "id" is expected to be a hexadecimal string representing an ObjectId. If the conversion from the hexadecimal string to an ObjectId fails, the "Id" field will not be modified. Example:
doc := &Doc{} hexId := "63632c7dfc826378c8abd802" doc.PutId(hexId) hex, _ := primitive.ObjectIDFromHex(hexId) require.Equal(t, hex, doc.Id)
Doc declaration:
type Doc struct { wgm.DefaultModel `bson:",inline"` Name string `bson:"name"` Age int `bson:"age"` } func (d *Doc) ColName() string { return "Docs" }
type FindPageOption ¶
type FindPageOption struct {
// contains filtered or unexported fields
}
func NewFindPageOption ¶
func NewFindPageOption() *FindPageOption
func (*FindPageOption) SetSelectField ¶
func (o *FindPageOption) SetSelectField(bson interface{}) *FindPageOption
SetSelectField param: fieldStr bson Description: 初始化Select, bson.M{"age": 1} means that only the age field is displayed bson.M{"age": 0} means to display other fields except age
func (*FindPageOption) SetSortField ¶
func (o *FindPageOption) SetSortField(field ...string) *FindPageOption
SetSortField param: field []string Description: 初始化SortField, {"age", "-name"}, first sort by age in ascending order, then sort by name in descending order