mongo

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: 8 Imported by: 0

README

mongo

mgo基础上封装mongodbd的客户端。


安装

go get -u github.com/zhufuyi/pkg/mongo


使用示例

点击查看示例


执行脚本生成新model。

cd mongo/demo

# 修改名称(文件名和内容同时修改)
./rename.sh ./ yourTableName

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrNotFound 错误
	ErrNotFound = mgo.ErrNotFound
)

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 InitMongo

func InitMongo(url string) (string, *mgo.Session, error)

InitMongo 初始化mongodb,如果不指定数据库,默认数据库名为test

func InitializeMongodb

func InitializeMongodb(url string) error

InitializeMongodb 初始化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 UpdatedTime

func UpdatedTime(update bson.M) bson.M

UpdatedTime 更新updatedAt时间

Types

type DefaultSession

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

DefaultSession 默认mgo的会话

func (*DefaultSession) Close

func (d *DefaultSession) Close()

Close 关闭连接

func (*DefaultSession) Count

func (d *DefaultSession) Count(collection string, selector bson.M) (int, error)

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

func (*DefaultSession) CountAll

func (d *DefaultSession) CountAll(collection string, selector bson.M) (int, error)

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

func (*DefaultSession) DeleteAll

func (d *DefaultSession) DeleteAll(collection string, selector bson.M) (int, error)

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

func (*DefaultSession) DeleteAllReal

func (d *DefaultSession) DeleteAllReal(collection string, selector bson.M) (int, error)

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

func (*DefaultSession) DeleteOne

func (d *DefaultSession) DeleteOne(collection string, selector bson.M) error

DeleteOne 标记性删除一条记录

func (*DefaultSession) DeleteOneReal

func (d *DefaultSession) DeleteOneReal(collection string, selector bson.M) error

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

func (*DefaultSession) EnsureIndex

func (d *DefaultSession) EnsureIndex(collection string, index mgo.Index) error

EnsureIndex 设置索引

func (*DefaultSession) EnsureIndexKey

func (d *DefaultSession) EnsureIndexKey(collection string, indexKeys ...string) error

EnsureIndexKey 设置索引key

func (*DefaultSession) FindAll

func (d *DefaultSession) FindAll(collection string, result interface{}, selector bson.M, fields bson.M, skip int, limit int, sort ...string) error

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

func (*DefaultSession) FindAndModify

func (d *DefaultSession) FindAndModify(collection string, result interface{}, selector bson.M, update bson.M) error

FindAndModify 更新并返回最新记录

func (*DefaultSession) FindOne

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

FindOne 查找一条记录

func (*DefaultSession) Insert

func (d *DefaultSession) Insert(collection string, a interface{}) error

Insert 插入一条新数据

func (*DefaultSession) Run

func (d *DefaultSession) Run(cmd interface{}, result interface{}) error

Run 执行

func (*DefaultSession) UpdateAll

func (d *DefaultSession) UpdateAll(collection string, selector bson.M, update bson.M) (int, error)

UpdateAll 更新所有匹配的记录

func (*DefaultSession) UpdateOne

func (d *DefaultSession) UpdateOne(collection string, selector bson.M, update bson.M) error

UpdateOne 更新一条记录

func (*DefaultSession) WithLog

func (d *DefaultSession) WithLog() Sessioner

WithLog 带mongo执行命令log输出

type PublicFields

type PublicFields struct {
	ID        bson.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 初始化

type Sessioner

type Sessioner interface {
	WithLog() Sessioner
	Close()
	Insert(collection string, a interface{}) error
	FindOne(collection string, result interface{}, selector bson.M, fields bson.M) error
	FindAll(collection string, result interface{}, selector bson.M, fields bson.M, skip int, limit int, sort ...string) error
	UpdateOne(collection string, selector bson.M, update bson.M) error
	UpdateAll(collection string, selector bson.M, update bson.M) (int, error)
	DeleteOne(collection string, selector bson.M) error
	DeleteAll(collection string, selector bson.M) (int, error)
	DeleteOneReal(collection string, selector bson.M) error
	DeleteAllReal(collection string, selector bson.M) (int, error)
	Count(collection string, selector bson.M) (int, error)
	CountAll(collection string, selector bson.M) (int, error)
	FindAndModify(collection string, result interface{}, selector bson.M, update bson.M) error
	EnsureIndexKey(collection string, indexKeys ...string) error
	EnsureIndex(collection string, index mgo.Index) error
	Run(cmd interface{}, result interface{}) error
}

Sessioner 提供外部调用的接口

func Clone

func Clone(mdbName string, ses *mgo.Session) Sessioner

Clone 获取新的session,使用前先调用函数InitializeMongodb初始化session,否则抛出异常

func GetSession

func GetSession() Sessioner

GetSession 获取新的session,使用前先调用函数InitializeMongodb初始化session,否则抛出异常

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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