mongo

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SortTypeASC 排序类型: 正序
	SortTypeASC = 1
	// SortTypeDESC 排序类型: 倒序
	SortTypeDESC = -1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AggregateOpe added in v1.0.1

type AggregateOpe string

AggregateOpe 聚合操作表达式

const (
	// AggregateOpeGroup 分组
	AggregateOpeGroup AggregateOpe = "$group"
	// AggregateOpeMatch 匹配
	AggregateOpeMatch AggregateOpe = "$match"
	// AggregateOpeSum 计算总和
	AggregateOpeSum AggregateOpe = "$sum"
	// AggregateOpeSort 排序
	AggregateOpeSort AggregateOpe = "$sort"
	// AggregateOpeAvg 计算平均值
	AggregateOpeAvg AggregateOpe = "$avg"
	// AggregateopeLimit 限制记录数
	AggregateopeLimit AggregateOpe = "$limit"
	// AggregateOpeSkip 跳过指定记录数
	AggregateOpeSkip AggregateOpe = "$skip"
	// AggregateOpeUnset 忽略相关字段
	AggregateOpeUnset AggregateOpe = "$unset"
	// AggregateOpeProject 只返回指定字段
	AggregateOpeProject AggregateOpe = "$project"
	// AggregateOpeFirst 获取第一个文档数据
	AggregateOpeFirst AggregateOpe = "$first"
	// AggregateOpeLast 获取最后一个文档数据
	AggregateOpeLast AggregateOpe = "$last"
)

func (AggregateOpe) String added in v1.0.1

func (a AggregateOpe) String() string

type Chain

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

func (*Chain) Aggregate added in v1.0.1

func (ch *Chain) Aggregate(des interface{}, stages ...bson.D) error

Aggregate 聚合操作 des: 聚合操作后的结果集(指针), stages: 对应的stage集合, 按顺序排列 stages可以调用chain.GetXXXStage获取, 如果不满足则自定义bson.D传入

func (*Chain) Comparison

func (ch *Chain) Comparison(field string, symbol Comparison, val interface{}) *Chain

Comparison 比较运算封装, field: 字段名, symbol: 比较符号, val: 比较值 such as: Comparison(age, ComparisonGt, 1), 筛选年龄大于1的

func (*Chain) Count

func (ch *Chain) Count() (int64, error)

Count 根据chain内的条件查询满足条件的文档记录数

func (*Chain) Delete

func (ch *Chain) Delete(ctx context.Context) error

Delete 根据chain的条件删除指定的文档

func (*Chain) DeleteOne

func (ch *Chain) DeleteOne(ctx context.Context) error

DeleteOne 根据chain的条件删除一条文档

func (*Chain) Eq

func (ch *Chain) Eq(field string, val interface{}) *Chain

Eq "等于"运算的条件拼接, field: 字段名, val: 比较值

func (*Chain) Exists

func (ch *Chain) Exists(field string, exist bool) *Chain

Exists 匹配具有指定字段的文档, field: 字段名, exist: 布尔值 such as: ch.Exists("name", true).Find(&des), 找出存在name字段的数据

func (*Chain) Filter

func (ch *Chain) Filter(filter map[string]interface{}) *Chain

Filter 多个查询条件

func (*Chain) Find

func (ch *Chain) Find(des interface{}) error

Find 查询多个文档

func (*Chain) FindOne

func (ch *Chain) FindOne(des interface{}) error

FindOne 查询单个文档

func (*Chain) GetAvgStage added in v1.0.1

func (ch *Chain) GetAvgStage(calledFiled, filed string) bson.D

such as: ch.GetGroupStage("name", ch.GetAvgStage("avg", "age")), 代表根据name字段分组, 计算每个分组age的平均值, 最终每个组的平均值命名为avg

func (*Chain) GetFirstStage added in v1.0.1

func (ch *Chain) GetFirstStage(calledFiled, filed string) bson.D

func (*Chain) GetGroupStage added in v1.0.1

func (ch *Chain) GetGroupStage(groupFiled string, subStages ...bson.D) bson.D

GetGroupStage 获取$group的stage groupFiled: 要分组的字段名, subStages: 子stage, 如果需要则传入 tips: 目前该库只提供少量stage支持, 也可以自定义bson传入

func (*Chain) GetLastStage added in v1.0.1

func (ch *Chain) GetLastStage(calledFiled, filed string) bson.D

func (*Chain) GetLimitStage added in v1.0.1

func (ch *Chain) GetLimitStage(num int64) bson.D

GetLimitStage 获取$limit的stage num: 限制的文档数

func (*Chain) GetMatchStage added in v1.0.1

func (ch *Chain) GetMatchStage(filed string, val interface{}) bson.D

GetMatchStage 获取$match的stage filed: 匹配的字段, val: 具体匹配值

func (*Chain) GetProjectStage added in v1.0.1

func (ch *Chain) GetProjectStage(fileds ...string) bson.D

func (*Chain) GetSkipStage added in v1.0.1

func (ch *Chain) GetSkipStage(num int64) bson.D

GetSkipStage 获取$skip的stage num: 要跳过的文档数

func (*Chain) GetSortStage added in v1.0.1

func (ch *Chain) GetSortStage(rules ...SortRule) bson.D

GetSortStage 获取$sort的stage rules: 具体的排序规则集合, 可参考mongo.SortRule

func (*Chain) GetSumStage added in v1.0.1

func (ch *Chain) GetSumStage(calledFiled, filed string) bson.D

such as: ch.GetGroupStage("name", ch.GetSumStage("total_sum", "age")), 代表根据name字段分组, 计算每个组age的总和, 最终每个组的总和命名为total_sum

func (*Chain) GetUnsetStage added in v1.0.1

func (ch *Chain) GetUnsetStage(fileds ...string) bson.D

GetUnsetStage 获取$unset的stage fileds: 要忽略的字段

func (*Chain) Gt

func (ch *Chain) Gt(field string, val int64) *Chain

Gt "大于"运算的条件拼接, field: 字段名, val: 比较值 such as: ch.Gt("age", 18).Find(&des), 找出年龄大于18岁的

func (*Chain) Gte

func (ch *Chain) Gte(field string, val int64) *Chain

Gte "大于等于"运算的条件拼接, field: 字段名, val: 比较值

func (*Chain) In

func (ch *Chain) In(field string, arrays []interface{}) *Chain

In 匹配数组中指定的任何值, field: 字段名, arrays: 数组 such as: ch.In("age", []interface{}{18, 19}).Find(&des), 找年龄为18和19岁的

func (*Chain) InInt64

func (ch *Chain) InInt64(field string, arrays []int64) *Chain

InInt64 匹配数组中指定的任何值, 数组类型为int64(语法糖), field: 字段名, arrays: 数组

func (*Chain) InString

func (ch *Chain) InString(field string, arrays []string) *Chain

InString 匹配数组中指定的任何值, 数组类型为string(语法糖), field: 字段名, arrays: 数组

func (*Chain) Inc

func (ch *Chain) Inc(field string, incNums int64) error

Inc 给多个文档字段增加或减少指定的数字 such as: ch.Where("name", "leslie").IncOne("age", 1) 代表给name为leslie的所有文档, age加一

func (*Chain) IncOne

func (ch *Chain) IncOne(field string, incNums int64) error

IncOne 给一个文档字段增加或减少指定的数字 such as: ch.Where("name", "leslie").IncOne("age", 1) 代表给name为leslie的单个文档, age加一

func (*Chain) InsertMany

func (ch *Chain) InsertMany(docs []interface{}) error

InsertMany 插入多条文档, 需要interface{}数组

func (*Chain) InsertOne

func (ch *Chain) InsertOne(doc interface{}) error

InsertOne 插入一条文档

func (*Chain) Limit

func (ch *Chain) Limit(limit int64) *Chain

Limit 指定查询返回的文档数

func (*Chain) Lt

func (ch *Chain) Lt(field string, val int64) *Chain

Lt "小于"运算的条件拼接, field: 字段名, val: 比较值

func (*Chain) Lte

func (ch *Chain) Lte(field string, val int64) *Chain

Lte "小于等于"运算的条件拼接, field: 字段名, val: 比较值

func (*Chain) NotEq

func (ch *Chain) NotEq(field string, val interface{}) *Chain

NotEq "不等于"运算的条件拼接, field: 字段名, val: 比较值

func (*Chain) NotIn

func (ch *Chain) NotIn(field string, arrays []interface{}) *Chain

NotIn 不匹配数组中指定的任何值, field: 字段名, arrays: 数组

func (*Chain) NotInInt64

func (ch *Chain) NotInInt64(field string, arrays []int64) *Chain

NotInInt64 不匹配数组中指定的任何值, 数组类型为int64(语法糖), field: 字段名, arrays: 数组

func (*Chain) NotInString

func (ch *Chain) NotInString(field string, arrays []string) *Chain

NotInString 不匹配数组中指定的任何值, 数组类型为string(语法糖), field: 字段名, arrays: 数组

func (*Chain) Or

func (ch *Chain) Or(filter map[string]interface{}) *Chain

Or 或运算 such as: ch.Or(map[string]interface{}{"name": "leslie", "age": 18}) 筛选name为leslie或者age为18的文档

func (*Chain) Ors

func (ch *Chain) Ors(filters ...map[string]interface{}) *Chain

Ors 或运算 具体例子参考test/chain_test.go.TestChainOrs用例

func (*Chain) Paginate

func (ch *Chain) Paginate(f *PageFilter, des interface{}) (err error)

Paginate 分页查询, f: 分页相关参数, 方法调用结束后会将总条数/总页数放入f内 des: 查询结果集

func (*Chain) Projection

func (ch *Chain) Projection(fileds ...string) *Chain

Projection 指定要返回的字段 such as: ch.Projection("name", "age") 只返回name和age字段

func (*Chain) Regex

func (ch *Chain) Regex(filed, content string) *Chain

Regex 模糊查找, filed: 查找的字段名, content: 模糊搜索内容 such as: ch.Regex("naem", "le") 查询name字段包含le的数据

func (*Chain) Skip

func (ch *Chain) Skip(skip int64) *Chain

Skip 跳过指定条数查询 such as: ch.Skip(1) 跳过第一条查询

func (*Chain) Sort

func (ch *Chain) Sort(rules ...SortRule) *Chain

Sort 根据条件进行排序 such as: ch.Sort(SortRule{Typ: mongo.SortTypeASC, Field: "value"}).Find(&list), 根据value升序查询

func (*Chain) Type

func (ch *Chain) Type(field string, typ MongodbType) *Chain

Type 如果字段属于指定类型,则选择文档

func (*Chain) Update

func (ch *Chain) Update(updateMap map[string]interface{}) error

Update 根据chain的条件更新指定的文档, updateMap为更新的内容

func (*Chain) UpdateOne

func (ch *Chain) UpdateOne(updateMap map[string]interface{}) error

UpdateOne 根据chain的条件更新指定的一条文档, updateMap为更新的内容

func (*Chain) UpsertOne

func (ch *Chain) UpsertOne(content, defaultContent map[string]interface{}) error

Upsert 根据chain内的查询条件暂存区,更新或新增一条记录 content: 要更新的内容 defaultContent: 如果没找到记录, 插入文档的字段默认值 such as: ch.Where("name", "leslie").UpsertOne(map[string]interface{}{"age": 18}, map[string]interface{}{"name", "leslie"}), 找到name为leslie的文档, 并将age更新为18, 如果没找到则新增一条文档, 默认值为name-leslie, age-18

func (*Chain) Where

func (ch *Chain) Where(key string, val interface{}) *Chain

Where 单个查询条件拼接

func (*Chain) WithCtx

func (ch *Chain) WithCtx(ctx context.Context) *Chain

type Collection

type Collection interface {
	// Collection 返回对应的集合名称
	Collection() string
}

type Comparison

type Comparison string
const (
	// ComparisonGt 大于比较符
	ComparisonGt Comparison = "$gt"
	// ComparisonGte 大于等于比较符
	ComparisonGte Comparison = "$gte"
	// ComparisonLt 小于比较符
	ComparisonLt Comparison = "$lt"
	// ComparisonLte 小于等于比较符号
	ComparisonLte Comparison = "$lte"
	// ComparisonIn 范围查询符号(匹配项)
	ComparisonIn Comparison = "$in"
	// ComparisonNotIn 范围查询符号(排除匹配项)
	ComparisonNotIn Comparison = "$nin"
	// ComparisonEq 等于比较符
	ComparisonEq Comparison = "$eq"
	// ComparisonNotEq 不等于比较符
	ComparisonNotEq Comparison = "$ne"
)

func (Comparison) String

func (c Comparison) String() string

type Conn

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

func NewConn

func NewConn(opts ...Option) (*Conn, func())

func (*Conn) Collection

func (c *Conn) Collection(i Collection) *Chain

Collection 获取操作集合的对象

func (*Conn) CreateIndex added in v1.0.3

func (c *Conn) CreateIndex(ctx context.Context, coll Collection, indexs ...SortRule) (string, error)

CreateIndex 在集合中创建索引 该命令返回索引的名称

coll: 集合接口,参考mongo.Collection, indexs: 索引规则,参考mongo.SortRule

func (*Conn) DbStats added in v1.0.2

func (c *Conn) DbStats(ctx context.Context, des interface{}) error

DbStats 用于获取数据库的统计信息 该命令返回的信息包括数据库的大小,对象的数量,索引的数量,以及其他统计信息

des: 命令执行结果, 结构体指针

func (*Conn) GetDB

func (c *Conn) GetDB() *mongo.Database

GetDB 获取go driver的database对象

func (*Conn) IsMaster added in v1.0.2

func (c *Conn) IsMaster(ctx context.Context, des interface{}) error

IsMaster 用于检查服务器的状态,特别是用于确定服务器是否是主节点(Primary) 是否是一个副本集(Replica Set),以及其他关于服务器的基本信息 这个命令可以帮助你了解当前 MongoDB 部署的状态和拓扑

des: 命令执行结果, 结构体指针

func (*Conn) Ping added in v1.0.2

func (c *Conn) Ping(ctx context.Context) error

Ping 用于检查服务器是否可用

func (*Conn) RunCommand added in v1.0.2

func (c *Conn) RunCommand(ctx context.Context, cmd bson.D, des interface{}) error

RunCommand 执行mongodb数据库管理命令 cmd: 命令集合, bson.D类型 des: 命令执行结果, 结构体指针

func (*Conn) ServerStatus added in v1.0.2

func (c *Conn) ServerStatus(ctx context.Context, des interface{}) error

ServerStatus 用于获取服务器的状态信息 该命令返回的信息包括服务器的版本,操作系统,内存使用情况,连接数,以及其他统计信息

des: 命令执行结果, 结构体指针

type Element

type Element string
const (
	// ElementExists 匹配具有指定字段的文档
	ElementExists Element = "$exists"
	// ElementType 如果字段属于指定类型,则选择文档
	ElementType Element = "$type"
)

func (Element) String

func (e Element) String() string

type MongodbType

type MongodbType int

MongodbType mongodb数据类型定义

const (
	Double MongodbType = iota + 1
	String
	Object
	Array
	BinaryData
	Undefined
	ObjectId
	Boolean
	Date
	Null
	RegularExpression
	JavaScript MongodbType = iota + 2
	Symbol
	Int32     MongodbType = 16
	Timestamp MongodbType = 17
	Int64     MongodbType = 18
)

type Option

type Option func(*config)

func WithAddr

func WithAddr(addrs ...string) Option

func WithCommandMonitor added in v1.0.4

func WithCommandMonitor(commandMonitor *event.CommandMonitor) Option

WithCommandMonitor set command monitor into config

func WithDatabase

func WithDatabase(dbName string) Option

func WithMaxPoolSize

func WithMaxPoolSize(maxPoolSize uint64) Option

func WithMinPoolSize added in v1.0.4

func WithMinPoolSize(minPoolSize uint64) Option

func WithMonitor added in v1.0.4

func WithMonitor(monitor interface{}) Option

WithMonitor set monitor into config monitor must be *event.PoolMonitor or *event.CommandMonitor if monitor isn't these type, panic

func WithPassword

func WithPassword(passwd string) Option

func WithPoolMonitor added in v1.0.4

func WithPoolMonitor(poolMonitor *event.PoolMonitor) Option

WithPoolMonitor set pool monitor into config

func WithServerMonitor added in v1.0.4

func WithServerMonitor(serverMonitor *event.ServerMonitor) Option

func WithUsername

func WithUsername(username string) Option

type PageFilter

type PageFilter struct {
	// 页数
	PageNum int64
	// 每页大小
	PageSize int64
	// 总条数
	TotalCount int64
	// 总页数
	TotalPage int64
}

type SortRule

type SortRule struct {
	// 排序类型
	Typ SortType
	// 排序字段名
	Field string
}

type SortType

type SortType int8

SortType 排序类型

Jump to

Keyboard shortcuts

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