mgo

package
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2020 License: Apache-2.0 Imports: 10 Imported by: 106

Documentation

Index

Constants

View Source
const (
	MaxLen int = 5000 //分配插入
)

Variables

View Source
var (
	MgoPool = pool.ClassicPool(
		config.MGO_CONN_CAP,
		config.MGO_CONN_CAP/5,
		func() (pool.Src, error) {
			if err != nil || session.Ping() != nil {
				if session != nil {
					session.Close()
				}
				Refresh()
			}
			return &MgoSrc{session.Clone()}, err
		},
		connGcSecond)
)

Functions

func Call

func Call(fn func(pool.Src) error) error

调用资源池中的资源

func Close

func Close()

销毁资源池

func CollectionNames

func CollectionNames(dbname string) (names []string, err error)

获取数据库集合列表

func DatabaseNames

func DatabaseNames() (names []string, err error)

获取所有数据

func Error

func Error() error

func Len

func Len() int

返回当前资源数量

func Mgo

func Mgo(resultPtr interface{}, operate string, option map[string]interface{}) error

增删改查操作的统一方法 count操作resultPtr类型为*int list操作resultPtr类型为*map[string][]string find操作resultPtr类型为*map[string]interface{} insert操作resultPtr类型为*[]string,允许为nil(不接收id列表) update操作resultPtr为nil remove操作resultPtr为nil

func Refresh

func Refresh()

Types

type Count

type Count struct {
	Database   string                 // 数据库
	Collection string                 // 集合
	Query      map[string]interface{} // 查询语句
}

传入数据库与集合名 | 返回文档总数

func (*Count) Exec

func (self *Count) Exec(resultPtr interface{}) (err error)

type Find

type Find struct {
	Database   string                 // 数据库
	Collection string                 // 集合
	Query      map[string]interface{} // 查询语句
	Sort       []string               // 排序,用法如Sort("firstname", "-lastname"),优先按firstname正序排列,其次按lastname倒序排列
	Skip       int                    // 跳过前n个文档
	Limit      int                    // 返回最多n个文档
	Select     interface{}            // 只查询、返回指定字段,如{"name":1}

}

在指定集合进行条件查询

func (*Find) Exec

func (self *Find) Exec(resultPtr interface{}) (err error)

type Insert

type Insert struct {
	Database   string                   // 数据库
	Collection string                   // 集合
	Docs       []map[string]interface{} // 文档
}

插入新数据

func (*Insert) Exec

func (self *Insert) Exec(resultPtr interface{}) (err error)

type List

type List struct {
	Dbs []string //数据库名称列表
}

传入数据库列表 | 返回数据库及其集合树

func (*List) Exec

func (self *List) Exec(resultPtr interface{}) (err error)

type MgoSrc

type MgoSrc struct {
	*mgo.Session
}

func (*MgoSrc) Close

func (self *MgoSrc) Close()

被资源池删除前的自毁方法

func (*MgoSrc) Reset

func (*MgoSrc) Reset()

使用后的重置方法

func (*MgoSrc) Usable

func (self *MgoSrc) Usable() bool

判断资源是否可用

type Operator

type Operator interface {
	Exec(resultPtr interface{}) (err error)
}

增删改查操作

type Remove

type Remove struct {
	Database   string                 // 数据库
	Collection string                 // 集合
	Selector   map[string]interface{} // 文档选择器
}

删除数据

func (*Remove) Exec

func (self *Remove) Exec(_ interface{}) error

type Update

type Update struct {
	Database   string                 // 数据库
	Collection string                 // 集合
	Selector   map[string]interface{} // 文档选择器
	Change     map[string]interface{} // 文档更新内容
}

更新第一个匹配的数据

func (*Update) Exec

func (self *Update) Exec(_ interface{}) error

type UpdateAll

type UpdateAll struct {
	Database   string                 // 数据库
	Collection string                 // 集合
	Selector   map[string]interface{} // 文档选择器
	Change     map[string]interface{} // 文档更新内容
}

更新全部匹配的数据

func (*UpdateAll) Exec

func (self *UpdateAll) Exec(resultPtr interface{}) (err error)

type Upsert

type Upsert struct {
	Database   string                 // 数据库
	Collection string                 // 集合
	Selector   map[string]interface{} // 文档选择器
	Change     map[string]interface{} // 文档更新内容
}

更新第一个匹配的数据,若无匹配项则插入

func (*Upsert) Exec

func (self *Upsert) Exec(resultPtr interface{}) (err error)

Jump to

Keyboard shortcuts

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