qmgo

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrNotFoundStr 错误
	ErrNotFoundStr = qmgo.ErrNoSuchDocuments.Error()
)

Functions

func CheckUpdateContent

func CheckUpdateContent(update bson.M) error

CheckUpdateContent 执行更新操作之前先判断有没有$操作符

func DeletedTime

func DeletedTime(update bson.M) bson.M

DeletedTime 更新deletedAt时间

func ExcludeDeleted

func ExcludeDeleted(selector bson.M) bson.M

ExcludeDeleted 不包含已删除的

func InitToGlobal

func InitToGlobal(url string) error

InitToGlobal 初始化mongodb,全局使用,只适用对单个mongodb操作,如果不指定数据库,默认数据库名为test

形式一:localhost 或 localhost:27017
形式二:mongodb://localhost:27017/database_name 或 mongodb://localhost1:port,localhost2:port/database_name
形式三:mongodb://user:password@localhost:27017/database_name 或 mongodb://user:password@localhost1:port,localhost2:port/database_name

func NewObjectID

func NewObjectID() primitive.ObjectID

NewObjectID 新文档id

func ObjectIDHex

func ObjectIDHex(s string) primitive.ObjectID

ObjectIDHex 字符串转为mongodb文档id

func UpdatedTime

func UpdatedTime(update bson.M) bson.M

UpdatedTime 更新updatedAt时间

Types

type Clienter

type Clienter interface {
	WithLog() Clienter
	Close() error
	Insert(collection string, doc interface{}) error
	InsertMany(collection string, docs []interface{}) error
	FindOne(collection string, result interface{}, query bson.M, fields bson.M) error
	FindAll(collection string, result interface{}, query bson.M, fields bson.M, skip int64, limit int64, sort ...string) error
	UpdateOne(collection string, query bson.M, update bson.M) error
	UpdateAll(collection string, query bson.M, update bson.M) (int64, error)
	DeleteOne(collection string, query bson.M) error
	DeleteAll(collection string, query bson.M) (int64, error)
	DeleteOneCompletely(collection string, query bson.M) error
	DeleteAllCompletely(collection string, query bson.M) (int64, error)
	Count(collection string, query bson.M) (int64, error)
	CountAll(collection string, query bson.M) (int64, error)
	EnsureIndexes(collection string, indexes []string) error
	EnsureUniques(collection string, uniques []string) error
	Aggregate(collection string, matchStage primitive.D, groupStage primitive.D) ([]bson.M, error)
	Transaction(collection string, callback func(sessCtx context.Context) (interface{}, error)) (interface{}, error)
	GetQmgoClient(collection string) *qmgo.QmgoClient
}

Clienter 提供外部调用的接口

func GetCli

func GetCli() Clienter

GetCli 获取新的session,使用前先调用函数InitToGlobal,否则抛出异常

func Init

func Init(url string) (string, Clienter, error)

Init 初始化mongodb,使用对多个mongodb独立操作,如果不指定数据库,默认数据库名为test

type DefaultClient

type DefaultClient struct {
	// contains filtered or unexported fields
}

DefaultClient mgo的客户端对象

func (*DefaultClient) Aggregate

func (d *DefaultClient) Aggregate(collection string, matchStage primitive.D, groupStage primitive.D) ([]bson.M, error)

Aggregate 聚合查询

func (*DefaultClient) Close

func (d *DefaultClient) Close() error

Close 关闭连接

func (*DefaultClient) Count

func (d *DefaultClient) Count(collection string, query bson.M) (int64, error)

Count 统计匹配的数量,不包括标记性删除的记录

func (*DefaultClient) CountAll

func (d *DefaultClient) CountAll(collection string, query bson.M) (int64, error)

CountAll 统计匹配的数量,包括标记性删除的记录

func (*DefaultClient) DeleteAll

func (d *DefaultClient) DeleteAll(collection string, query bson.M) (int64, error)

DeleteAll 标记性删除所有匹配的记录

func (*DefaultClient) DeleteAllCompletely

func (d *DefaultClient) DeleteAllCompletely(collection string, query bson.M) (int64, error)

DeleteAllCompletely 删除所有匹配的记录,包括标记性删除的记录

func (*DefaultClient) DeleteOne

func (d *DefaultClient) DeleteOne(collection string, query bson.M) error

DeleteOne 标记性删除一条记录

func (*DefaultClient) DeleteOneCompletely

func (d *DefaultClient) DeleteOneCompletely(collection string, query bson.M) error

DeleteOneCompletely 删除一条记录,包括标记性删除的记录

func (*DefaultClient) EnsureIndexes

func (d *DefaultClient) EnsureIndexes(collection string, indexes []string) error

EnsureIndexes 设置普通索引

func (*DefaultClient) EnsureUniques

func (d *DefaultClient) EnsureUniques(collection string, uniques []string) error

EnsureUniques 设置唯一索引

func (*DefaultClient) FindAll

func (d *DefaultClient) FindAll(collection string, result interface{}, query bson.M, fields bson.M, page int64, limit int64, sort ...string) error

FindAll 查找符合条件的多条记录,这里参数skip表示页码,limit表示每页多少行数据

func (*DefaultClient) FindOne

func (d *DefaultClient) FindOne(collection string, result interface{}, query bson.M, fields bson.M) error

FindOne 查找一条记录

func (*DefaultClient) GetQmgoClient

func (d *DefaultClient) GetQmgoClient(collection string) *qmgo.QmgoClient

GetQmgoClient 获取client对象

func (*DefaultClient) Insert

func (d *DefaultClient) Insert(collection string, doc interface{}) error

Insert 插入一条新数据

func (*DefaultClient) InsertMany

func (d *DefaultClient) InsertMany(collection string, docs []interface{}) error

InsertMany 插入多条新数据

func (*DefaultClient) Transaction

func (d *DefaultClient) Transaction(collection string, callback func(sessCtx context.Context) (interface{}, error)) (interface{}, error)

Transaction 事务

func (*DefaultClient) UpdateAll

func (d *DefaultClient) UpdateAll(collection string, query bson.M, update bson.M) (int64, error)

UpdateAll 更新所有匹配的记录

func (*DefaultClient) UpdateOne

func (d *DefaultClient) UpdateOne(collection string, query bson.M, update bson.M) error

UpdateOne 更新一条记录

func (*DefaultClient) WithLog

func (d *DefaultClient) WithLog() Clienter

WithLog 打印执行命令

type PublicFields

type PublicFields struct {
	ID        primitive.ObjectID `bson:"_id" json:"id"`                                  // 唯一ID
	CreatedAt time.Time          `bson:"createdAt" json:"createdAt"`                     // 创建时间
	UpdatedAt time.Time          `bson:"updatedAt" json:"updatedAt"`                     // 修改时间
	DeletedAt *time.Time         `bson:"deletedAt,omitempty" json:"deletedAt,omitempty"` // 删除时间
}

PublicFields 公共字段

func (*PublicFields) SetFieldsValue

func (p *PublicFields) SetFieldsValue()

SetFieldsValue 设置公共字段值,在插入数据时使用

type PublicFieldsInt

type PublicFieldsInt struct {
	ID        int64      `bson:"_id" json:"id,string"`                           // 唯一ID
	CreatedAt time.Time  `bson:"createdAt" json:"createdAt"`                     // 创建时间
	UpdatedAt time.Time  `bson:"updatedAt" json:"updatedAt"`                     // 修改时间
	DeletedAt *time.Time `bson:"deletedAt,omitempty" json:"deletedAt,omitempty"` // 删除时间
}

PublicFieldsInt 设置公共字段值,自定id,在插入数据时使用

func (*PublicFieldsInt) SetFieldsValue

func (m *PublicFieldsInt) SetFieldsValue(newID int64)

SetFieldsValue 初始化

Jump to

Keyboard shortcuts

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