mongodb

package
v0.0.0-...-d31617f Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Filter

type Filter struct {
	AndFilter bson.D
	OrFilter  bson.A
}

Filter builder

func NewFilter

func NewFilter() *Filter

func (*Filter) AppendAnd

func (w *Filter) AppendAnd(cond bool, field string, operate string, args ...interface{}) *Filter

* append与查询条件 * * param cond 是否加入条件 * param field 字段名 * param operate 操作符(eq, gt, gte, in, lt, lte, ne, nin),无需带$符号 * - eq:等于, gt:大于, gte:大于或等于, lt:小于, lte:小于或等于 * - ne:不等于, in:在数组中, nin:不在数组中 * - 其它详见:https://docs.mongodb.com/manual/reference/operator/query/ * param args 字段值 * * return Filter对象

func (*Filter) AppendOr

func (w *Filter) AppendOr(cond bool, field string, operate string, args ...interface{}) *Filter

* append或查询条件 * * param cond 是否加入条件 * param field 字段名 * param operate 操作符(eq, gt, gte, in, lt, lte, ne, nin),无需带$符号 * - eq:等于, gt:大于, gte:大于或等于, lt:小于, lte:小于或等于 * - ne:不等于, in:在数组中, nin:不在数组中 * - 其它详见:https://docs.mongodb.com/manual/reference/operator/query/ * param args 字段值 * * return Filter对象

func (Filter) Build

func (w Filter) Build() bson.D

type MgoContext

type MgoContext struct {
	Name    string `mapstructure:"name"`
	Address string `mapstructure:"address"`
	// contains filtered or unexported fields
}

func GetContext

func GetContext(mKey string, dbName string) (*MgoContext, error)

* 获取mongo操作对象 * * param mKey 标识名称,对应为config.toml中mongo.db的name值 * param dbName 数据库名称,传空值则用连接字符串中的数据库名 * return mongo操作对象

func NewContext

func NewContext(address string, dbName string) (*MgoContext, error)

* 构建mongo操作对象 * * param address 连接字符串,格式:mongodb://user:pass@host:port/database?options * param dbName 数据库名称,传空值则用连接字符串中的数据库名 * return mongo操作对象

func (MgoContext) Close

func (mtx MgoContext) Close()

关闭连接

func (MgoContext) CollectionExist

func (mtx MgoContext) CollectionExist(tbName string) (bool, error)

* 判断Collection是否存在 * * param tbName Collection名称 * return 是否存在

func (MgoContext) Count

func (mtx MgoContext) Count(tbName string, filter interface{}) (int64, error)

* 统计记录数量 * bson类型,包名:"go.mongodb.org/mongo-driver/bson" * * param tbName Collection名称 * param filter 查询条件, 仅支持bson类型,bson类型示例如下 * 示例1: bson.M{"Title": "The Polyglot Developer Podcast"} * 示例2: bson.D{{"UpdateTime", bson.D{{"$lt", time.Now().Add(-5 * time.Minute)}}}} * (更多检索帮助,详见:https://docs.mongodb.com/manual/reference/operator/query/) * * return 记录行数

func (MgoContext) CreateCollection

func (mtx MgoContext) CreateCollection(tbName string) error

* 创建Collection * * param tbName Collection名称 * return 是否成功

func (MgoContext) CreateIndex

func (mtx MgoContext) CreateIndex(tbName string, uniqueKeys [][]string, indexKeys [][]string) error

* 创建索引 * * param tbName Collection名称 * param uniqueKeys 唯一键(参与字段名) * param indexKeys 索引(参与字段名) * return 是否成功

func (MgoContext) CreateShard

func (mtx MgoContext) CreateShard(dbName string, tbName string, shardKeys []string) error

* 创建分区 * * param tbName Collection名称 * param shardKeys 分区(参与字段名) * return 是否成功

func (MgoContext) Delete

func (mtx MgoContext) Delete(tbName string, filter interface{}) (int64, error)

* 删除数据 * bson类型,包名:"go.mongodb.org/mongo-driver/bson" * * param tbName Collection名称 * param filter 删除条件, 仅支持bson类型,bson类型示例如下 * 示例1: bson.M{"Title": "The Polyglot Developer Podcast"} * 示例2: bson.D{{"UpdateTime", bson.D{{"$lt", time.Now().Add(-5 * time.Minute)}}}} * (更多检索帮助,详见:https://docs.mongodb.com/manual/reference/operator/query/) * * return 删除成功数量

func (MgoContext) Distinct

func (mtx MgoContext) Distinct(tbName string, fieldName string, filter interface{}) ([]interface{}, error)

* 去重查询 * bson类型,包名:"go.mongodb.org/mongo-driver/bson" * * param tbName Collection名称 * param fieldName 结果字段名 * param filter 查询条件, 仅支持bson类型,bson类型示例如下 * 示例1: bson.M{"Title": "The Polyglot Developer Podcast"} * 示例2: bson.D{{"UpdateTime", bson.D{{"$lt", time.Now().Add(-5 * time.Minute)}}}} * (更多检索帮助,详见:https://docs.mongodb.com/manual/reference/operator/query/) * * return 返回去重后的结果字段值

func (MgoContext) Group

func (mtx MgoContext) Group(rowsSlicePtr interface{}, tbName string, filter interface{}, groupBy interface{}, columnBy map[string]map[string]interface{}, sorts map[string]string) error

* 分组统计 * bson类型,包名:"go.mongodb.org/mongo-driver/bson" * * param rowsSlicePtr 返回记录行数据对象, 支持[]struct, []map[string]interface{}类型,struct结构示例如下 * type Data struct { * ID int64 `bson:"ID,omitempty"` //标签bson定义表字段名称 * } * param tbName Collection名称 * param filter 过滤条件, 仅支持bson类型,bson类型示例如下 * 示例1: bson.M{"Title": "The Polyglot Developer Podcast"} * 示例2: bson.D{{"UpdateTime", bson.D{{"$lt", time.Now().Add(-5 * time.Minute)}}}} * (更多检索帮助,详见:https://docs.mongodb.com/manual/reference/operator/query/) * * param groupBy 分组字段名, 对应返回记录行数据对象的字段名为_id, 参考文档(Field=_id):https://docs.mongodb.com/manual/reference/operator/aggregation/group/#pipe._S_group * param columnBy 分组统计项map体, map体对应的JSON格式为: {"name":{"type":"key"}} * - name自定义统计项名称 * - type取值范围为:$avg, $first, $last, $max, $min, $sum, 其它详见官方文档:https://docs.mongodb.com/manual/reference/operator/aggregation/group/#accumulator-operator * - key为字段名(也可为bson类型定义), 参考文档(Field=field):https://docs.mongodb.com/manual/reference/operator/aggregation/group/#examples * * param sorts 排序map体 * - map体对应的JSON格式为: {"key":"type"} * - key为字段名,type取值范围为:desc(降序), asc(升序) * * return 返回是否异常

func (MgoContext) Inserts

func (mtx MgoContext) Inserts(tbName string, datas []interface{}) (int, error)

* 插入数据,支持单个或批量 * bson类型,包名:"go.mongodb.org/mongo-driver/bson" * * param tbName Collection名称 * param datas 入库数据,支持struct或bson类型,struct结构示例如下 * type Data struct { * ID int64 `bson:"ID,omitempty"` //标签bson定义表字段名称 * } * * return 入库成功数量

func (MgoContext) Ping

func (mtx MgoContext) Ping() error

Ping

func (MgoContext) Search

func (mtx MgoContext) Search(rowsSlicePtr interface{}, tbName string, filter interface{}, offset int64, limit int64, sorts map[string]string) error

* 检索数据 * bson类型,包名:"go.mongodb.org/mongo-driver/bson" * * param rowsSlicePtr 返回记录行数据对象, 支持[]struct, []map[string]interface{}类型,struct结构示例如下 * type Data struct { * ID int64 `bson:"ID,omitempty"` //标签bson定义表字段名称 * } * param tbName Collection名称 * param filter 查询条件, 仅支持bson类型,bson类型示例如下 * 示例1: bson.M{"Title": "The Polyglot Developer Podcast"} * 示例2: bson.D{{"UpdateTime", bson.D{{"$lt", time.Now().Add(-5 * time.Minute)}}}} * (更多检索帮助,详见:https://docs.mongodb.com/manual/reference/operator/query/) * * param offset 记录开始序号(0开始) * param limit 返回记录条数 * param sorts 排序map体 * - map体对应的JSON格式为: {"key":"type"} * - key为字段名,type取值范围为:desc(降序), asc(升序) * return 返回是否异常

func (MgoContext) Update

func (mtx MgoContext) Update(tbName string, filter interface{}, data map[string]interface{}, isUpsert bool) (map[string]int64, error)

* 更新数据 * bson类型,包名:"go.mongodb.org/mongo-driver/bson" * * param tbName Collection名称 * param filter 更新条件, 仅支持bson类型,bson类型示例如下 * 示例1: bson.M{"Title": "The Polyglot Developer Podcast"} * 示例2: bson.D{{"UpdateTime", bson.D{{"$lt", time.Now().Add(-5 * time.Minute)}}}} * (更多检索帮助,详见:https://docs.mongodb.com/manual/reference/operator/query/) * * param data 更新数据,map类型 * - map体对应的JSON格式为: {"key":"value"} * - key为字段名,value字段值 * * param isUpsert 是否不存在时插入数据 * * return 入库成功数量

Jump to

Keyboard shortcuts

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