Documentation ¶
Index ¶
- Variables
- func CheckUpdateContent(update bson.M) error
- func DeletedTime(update bson.M) bson.M
- func ExcludeDeleted(selector bson.M) bson.M
- func InitMongo(url string) (string, *mgo.Session, error)
- func InitializeMongodb(url string) error
- func UpdatedTime(update bson.M) bson.M
- type DefaultSession
- func (d *DefaultSession) Close()
- func (d *DefaultSession) Count(collection string, selector bson.M) (int, error)
- func (d *DefaultSession) CountAll(collection string, selector bson.M) (int, error)
- func (d *DefaultSession) DeleteAll(collection string, selector bson.M) (int, error)
- func (d *DefaultSession) DeleteAllReal(collection string, selector bson.M) (int, error)
- func (d *DefaultSession) DeleteOne(collection string, selector bson.M) error
- func (d *DefaultSession) DeleteOneReal(collection string, selector bson.M) error
- func (d *DefaultSession) EnsureIndex(collection string, index mgo.Index) error
- func (d *DefaultSession) EnsureIndexKey(collection string, indexKeys ...string) error
- func (d *DefaultSession) FindAll(collection string, result interface{}, selector bson.M, fields bson.M, ...) error
- func (d *DefaultSession) FindAndModify(collection string, result interface{}, selector bson.M, update bson.M) error
- func (d *DefaultSession) FindOne(collection string, result interface{}, selector bson.M, fields bson.M) error
- func (d *DefaultSession) Insert(collection string, a interface{}) error
- func (d *DefaultSession) Run(cmd interface{}, result interface{}) error
- func (d *DefaultSession) UpdateAll(collection string, selector bson.M, update bson.M) (int, error)
- func (d *DefaultSession) UpdateOne(collection string, selector bson.M, update bson.M) error
- func (d *DefaultSession) WithLog() Sessioner
- type PublicFields
- type PublicFieldsInt
- type Sessioner
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrNotFound 错误 ErrNotFound = mgo.ErrNotFound )
Functions ¶
func CheckUpdateContent ¶
CheckUpdateContent 执行更新操作之前先判断有没有$操作符
func InitializeMongodb ¶
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
Types ¶
type DefaultSession ¶
type DefaultSession struct {
// contains filtered or unexported fields
}
DefaultSession 默认mgo的会话
func (*DefaultSession) DeleteAllReal ¶
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) 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 GetSession ¶
func GetSession() Sessioner
GetSession 获取新的session,使用前先调用函数InitializeMongodb初始化session,否则抛出异常
Click to show internal directories.
Click to hide internal directories.