Documentation ¶
Index ¶
- Constants
- type AggregateOpe
- type Chain
- func (ch *Chain) Aggregate(des interface{}, stages ...bson.D) error
- func (ch *Chain) Comparison(field string, symbol Comparison, val interface{}) *Chain
- func (ch *Chain) Count() (int64, error)
- func (ch *Chain) Debug(w io.WriteCloser) *Chain
- func (ch *Chain) Delete() error
- func (ch *Chain) DeleteOne() error
- func (ch *Chain) Eq(field string, val interface{}) *Chain
- func (ch *Chain) Exists(field string, exist bool) *Chain
- func (ch *Chain) Filter(filter map[string]interface{}) *Chain
- func (ch *Chain) Find(des interface{}) error
- func (ch *Chain) FindOne(des interface{}) error
- func (ch *Chain) GetAvgStage(calledFiled, filed string) bson.D
- func (ch *Chain) GetFirstStage(calledFiled, filed string) bson.D
- func (ch *Chain) GetGroupStage(groupFiled string, subStages ...bson.D) bson.D
- func (ch *Chain) GetLastStage(calledFiled, filed string) bson.D
- func (ch *Chain) GetLimitStage(num int64) bson.D
- func (ch *Chain) GetMatchStage(filed string, val interface{}) bson.D
- func (ch *Chain) GetProjectStage(fileds ...string) bson.D
- func (ch *Chain) GetSkipStage(num int64) bson.D
- func (ch *Chain) GetSortStage(rules ...SortRule) bson.D
- func (ch *Chain) GetSumStage(calledFiled, filed string) bson.D
- func (ch *Chain) GetUnsetStage(fileds ...string) bson.D
- func (ch *Chain) Gt(field string, val int64) *Chain
- func (ch *Chain) Gte(field string, val int64) *Chain
- func (ch *Chain) In(field string, arrays []interface{}) *Chain
- func (ch *Chain) InInt64(field string, arrays []int64) *Chain
- func (ch *Chain) InString(field string, arrays []string) *Chain
- func (ch *Chain) Inc(field string, incNums int64) error
- func (ch *Chain) IncOne(field string, incNums int64) error
- func (ch *Chain) InsertMany(docs []interface{}) error
- func (ch *Chain) InsertOne(doc interface{}) error
- func (ch *Chain) Limit(limit int64) *Chain
- func (ch *Chain) Lt(field string, val int64) *Chain
- func (ch *Chain) Lte(field string, val int64) *Chain
- func (ch *Chain) NotEq(field string, val interface{}) *Chain
- func (ch *Chain) NotIn(field string, arrays []interface{}) *Chain
- func (ch *Chain) NotInInt64(field string, arrays []int64) *Chain
- func (ch *Chain) NotInString(field string, arrays []string) *Chain
- func (ch *Chain) Or(filter map[string]interface{}) *Chain
- func (ch *Chain) Ors(filters ...map[string]interface{}) *Chain
- func (ch *Chain) Paginate(f *PageFilter, des interface{}) (err error)
- func (ch *Chain) Projection(fileds ...string) *Chain
- func (ch *Chain) Regex(filed, content string) *Chain
- func (ch *Chain) Skip(skip int64) *Chain
- func (ch *Chain) Sort(rules ...SortRule) *Chain
- func (ch *Chain) Type(field string, typ MongodbType) *Chain
- func (ch *Chain) Update(updateMap map[string]interface{}) error
- func (ch *Chain) UpdateOne(updateMap map[string]interface{}) error
- func (ch *Chain) UpsertOne(content, defaultContent map[string]interface{}) error
- func (ch *Chain) Where(key string, val interface{}) *Chain
- func (ch *Chain) WithCtx(ctx context.Context) *Chain
- type Collection
- type Comparison
- type Conn
- func (c *Conn) Collection(i Collection) *Chain
- func (c *Conn) CreateIndex(ctx context.Context, coll Collection, indexs ...SortRule) (string, error)
- func (c *Conn) DbStats(ctx context.Context, des interface{}) error
- func (c *Conn) GetDB() *mongo.Database
- func (c *Conn) IsMaster(ctx context.Context, des interface{}) error
- func (c *Conn) Ping(ctx context.Context) error
- func (c *Conn) RunCommand(ctx context.Context, cmd bson.D, des interface{}) error
- func (c *Conn) ServerStatus(ctx context.Context, des interface{}) error
- type Element
- type MongodbType
- type Option
- func WithAddr(addrs ...string) Option
- func WithCommandMonitor(commandMonitor *event.CommandMonitor) Option
- func WithDatabase(dbName string) Option
- func WithLoggerOptions(logOpt *options.LoggerOptions) Option
- func WithMaxPoolSize(maxPoolSize uint64) Option
- func WithMinPoolSize(minPoolSize uint64) Option
- func WithMonitor(monitor interface{}) Option
- func WithPassword(passwd string) Option
- func WithPoolMonitor(poolMonitor *event.PoolMonitor) Option
- func WithServerMonitor(serverMonitor *event.ServerMonitor) Option
- func WithUsername(username string) Option
- type PageFilter
- type SortRule
- type SortType
- type Statement
Constants ¶
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
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) Debug ¶ added in v1.0.7
func (ch *Chain) Debug(w io.WriteCloser) *Chain
Currently supported operations include Find, FindOne, InsertOne, InsertMany, UpdateOne, UpdateMany, DeleteOne, and DeleteMany. For more examples, refer to test/debug_test.go.
func (*Chain) Exists ¶
Exists 匹配具有指定字段的文档, field: 字段名, exist: 布尔值 such as: ch.Exists("name", true).Find(&des), 找出存在name字段的数据
func (*Chain) GetAvgStage ¶ added in v1.0.1
such as: ch.GetGroupStage("name", ch.GetAvgStage("avg", "age")), 代表根据name字段分组, 计算每个分组age的平均值, 最终每个组的平均值命名为avg
func (*Chain) GetFirstStage ¶ added in v1.0.1
func (*Chain) GetGroupStage ¶ added in v1.0.1
GetGroupStage 获取$group的stage groupFiled: 要分组的字段名, subStages: 子stage, 如果需要则传入 tips: 目前该库只提供少量stage支持, 也可以自定义bson传入
func (*Chain) GetLastStage ¶ added in v1.0.1
func (*Chain) GetLimitStage ¶ added in v1.0.1
GetLimitStage 获取$limit的stage num: 限制的文档数
func (*Chain) GetMatchStage ¶ added in v1.0.1
GetMatchStage 获取$match的stage filed: 匹配的字段, val: 具体匹配值
func (*Chain) GetProjectStage ¶ added in v1.0.1
func (*Chain) GetSkipStage ¶ added in v1.0.1
GetSkipStage 获取$skip的stage num: 要跳过的文档数
func (*Chain) GetSortStage ¶ added in v1.0.1
GetSortStage 获取$sort的stage rules: 具体的排序规则集合, 可参考mongo.SortRule
func (*Chain) GetSumStage ¶ added in v1.0.1
such as: ch.GetGroupStage("name", ch.GetSumStage("total_sum", "age")), 代表根据name字段分组, 计算每个组age的总和, 最终每个组的总和命名为total_sum
func (*Chain) GetUnsetStage ¶ added in v1.0.1
GetUnsetStage 获取$unset的stage fileds: 要忽略的字段
func (*Chain) Gt ¶
Gt "大于"运算的条件拼接, field: 字段名, val: 比较值 such as: ch.Gt("age", 18).Find(&des), 找出年龄大于18岁的
func (*Chain) In ¶
In 匹配数组中指定的任何值, field: 字段名, arrays: 数组 such as: ch.In("age", []interface{}{18, 19}).Find(&des), 找年龄为18和19岁的
func (*Chain) Inc ¶
Inc 给多个文档字段增加或减少指定的数字 such as: ch.Where("name", "leslie").IncOne("age", 1) 代表给name为leslie的所有文档, age加一
func (*Chain) IncOne ¶
IncOne 给一个文档字段增加或减少指定的数字 such as: ch.Where("name", "leslie").IncOne("age", 1) 代表给name为leslie的单个文档, age加一
func (*Chain) InsertMany ¶
InsertMany 插入多条文档, 需要interface{}数组
func (*Chain) NotInInt64 ¶
NotInInt64 不匹配数组中指定的任何值, 数组类型为int64(语法糖), field: 字段名, arrays: 数组
func (*Chain) NotInString ¶
NotInString 不匹配数组中指定的任何值, 数组类型为string(语法糖), field: 字段名, arrays: 数组
func (*Chain) Or ¶
Or 或运算 such as: ch.Or(map[string]interface{}{"name": "leslie", "age": 18}) 筛选name为leslie或者age为18的文档
func (*Chain) Paginate ¶
func (ch *Chain) Paginate(f *PageFilter, des interface{}) (err error)
Paginate 分页查询, f: 分页相关参数, 方法调用结束后会将总条数/总页数放入f内 des: 查询结果集
func (*Chain) Projection ¶
Projection 指定要返回的字段 such as: ch.Projection("name", "age") 只返回name和age字段
func (*Chain) Regex ¶
Regex 模糊查找, filed: 查找的字段名, content: 模糊搜索内容 such as: ch.Regex("naem", "le") 查询name字段包含le的数据
func (*Chain) Sort ¶
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) UpsertOne ¶
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
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 (*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
DbStats 用于获取数据库的统计信息 该命令返回的信息包括数据库的大小,对象的数量,索引的数量,以及其他统计信息
des: 命令执行结果, 结构体指针
func (*Conn) IsMaster ¶ added in v1.0.2
IsMaster 用于检查服务器的状态,特别是用于确定服务器是否是主节点(Primary) 是否是一个副本集(Replica Set),以及其他关于服务器的基本信息 这个命令可以帮助你了解当前 MongoDB 部署的状态和拓扑
des: 命令执行结果, 结构体指针
func (*Conn) RunCommand ¶ added in v1.0.2
RunCommand 执行mongodb数据库管理命令 cmd: 命令集合, bson.D类型 des: 命令执行结果, 结构体指针
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 WithCommandMonitor ¶ added in v1.0.4
func WithCommandMonitor(commandMonitor *event.CommandMonitor) Option
WithCommandMonitor set command monitor into config
func WithDatabase ¶
func WithLoggerOptions ¶ added in v1.0.6
func WithLoggerOptions(logOpt *options.LoggerOptions) Option
WithLoggerOptions set logger options into config
func WithMaxPoolSize ¶
func WithMinPoolSize ¶ added in v1.0.4
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 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