query

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OperatorEq  = "$eq"  // 等于
	OperatorNe  = "$ne"  // 不等于
	OperatorGt  = "$gt"  // 大于
	OperatorGte = "$gte" // 大于等于
	OperatorLt  = "$lt"  // 小于
	OperatorLte = "$lte" // 小于等于

	OperatorAnd = "$and" // 与
	OperatorOr  = "$or"  // 或
	OperatorNot = "$not" // 非
	OperatorNor = "$nor" // 非或

	OperatorExists = "$exists" // 是否存在
	OperatorType   = "$type"   // 类型

	OperatorExpr       = "$expr"       // 表达式
	OperatorJsonSchema = "$jsonSchema" // JSON Schema 验证
	OperatorMod        = "$mod"        // 取模
	OperatorRegex      = "$regex"      // 正则表达式
	OperatorText       = "$text"       // 文本搜索
	OperatorWhere      = "$where"      // JavaScript 表达式
	OperatorSearch     = "$search"     // 文本搜索

	OperatorAll       = "$all"       // 匹配所有
	OperatorElemMatch = "$elemMatch" // 匹配数组中的元素
	OperatorSize      = "$size"      // 数组大小

	OperatorIn  = "$in"  // 包含
	OperatorNin = "$nin" // 不包含

	OperatorSet         = "$set"         // 设置字段值
	OperatorUnset       = "$unset"       // 删除字段
	OperatorInc         = "$inc"         // 增加值
	OperatorMul         = "$mul"         // 乘法
	OperatorRename      = "$rename"      // 重命名字段
	OperatorCurrentDate = "$currentDate" // 设置当前日期
	OperatorAddToSet    = "$addToSet"    // 添加到集合
	OperatorPop         = "$pop"         // 删除数组中的元素
	OperatorPull        = "$pull"        // 删除匹配的数组元素
	OperatorPush        = "$push"        // 添加数组元素
	OperatorEach        = "$each"        // 批量添加数组元素
	OperatorSlice       = "$slice"       // 截取数组
	OperatorSort        = "$sort"        // 排序数组
	OperatorPosition    = "$position"    // 指定数组位置

	OperatorGroup       = "$group"       // 分组
	OperatorMatch       = "$match"       // 匹配
	OperatorProject     = "$project"     // 投影
	OperatorSortAgg     = "$sort"        // 排序
	OperatorLimit       = "$limit"       // 限制
	OperatorSkip        = "$skip"        // 跳过
	OperatorUnwind      = "$unwind"      // 拆分数组
	OperatorLookup      = "$lookup"      // 关联查询
	OperatorAddFields   = "$addFields"   // 添加字段
	OperatorReplaceRoot = "$replaceRoot" // 替换根字段
	OperatorCount       = "$count"       // 计数
	OperatorFacet       = "$facet"       // 多面查询
	OperatorBucket      = "$bucket"      // 分桶
	OperatorBucketAuto  = "$bucketAuto"  // 自动分桶
	OperatorIndexStats  = "$indexStats"  // 索引统计
	OperatorOut         = "$out"         // 输出
	OperatorMerge       = "$merge"       // 合并
	OperatorSum         = "$sum"         // 求和
	OperatorAvg         = "$avg"         // 平均值
	OperatorMin         = "$min"         // 最小值
	OperatorMax         = "$max"         // 最大值
	OperatorFirst       = "$first"       // 第一个值
	OperatorLast        = "$last"        // 最后一个值
	OperatorStdDevPop   = "$stdDevPop"   // 总体标准差
	OperatorStdDevSamp  = "$stdDevSamp"  // 样本标准差

	OperatorToLong    = "$toLong"    // 转换为 long 类型
	OperatorToDouble  = "$toDouble"  // 转换为 double 类型
	OperatorToDecimal = "$toDecimal" // 转换为 decimal 类型
	OperatorToString  = "$toString"  // 转换为 string 类型
	OperatorToDate    = "$toDate"    // 转换为 date 类型
	OperatorToInt     = "$toInt"     // 转换为 int 类型

	OperatorNear          = "$near"          // 查询距离某点最近的文档
	OperatorNearSphere    = "$nearSphere"    // 查询距离某点最近的文档(球面距离)
	OperatorGeoWithin     = "$geoWithin"     // 地理范围查询
	OperatorGeoIntersects = "$geoIntersects" // 地理相交查询

	OperatorGeometry    = "$geometry"    // 几何图形
	OperatorMaxDistance = "$maxDistance" // 最大距离
	OperatorMinDistance = "$minDistance" // 最小距离
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

Builder 用于构建 MongoDB 查询

func NewQueryBuilder

func NewQueryBuilder() *Builder

func (*Builder) AddStage

func (qb *Builder) AddStage(stage bsonV2.D) *Builder

AddStage 添加聚合阶段到管道

func (*Builder) Build

func (qb *Builder) Build() (bsonV2.M, *optionsV2.FindOptions)

Build 返回最终的过滤条件和查询选项

func (*Builder) BuildFind

func (qb *Builder) BuildFind() (interface{}, optionsV2.Lister[optionsV2.FindOptions], error)

BuildFind 构建用于 Find 的 filter 与 options 返回值:filter interface{} (可为 bson.M/bson.D/...), options.Lister[options.FindOptions], error

func (*Builder) BuildFindOne

func (qb *Builder) BuildFindOne() (interface{}, optionsV2.Lister[optionsV2.FindOneOptions], error)

BuildFindOne 构建用于 FindOne 的 filter 与 options 返回值:filter interface{} (可为 bson.M/bson.D/...), options.Lister[options.FindOneOptions], error

func (*Builder) BuildPipeline

func (qb *Builder) BuildPipeline() []bsonV2.D

BuildPipeline 返回最终的聚合管道

func (*Builder) SetAll

func (qb *Builder) SetAll(field string, values []interface{}) *Builder

SetAll 设置字段必须包含所有指定值的条件

func (*Builder) SetAnd

func (qb *Builder) SetAnd(conditions []bsonV2.M) *Builder

SetAnd 设置多个条件的逻辑与

func (*Builder) SetBetween

func (qb *Builder) SetBetween(field string, start, end interface{}) *Builder

SetBetween 设置字段的范围查询条件

func (*Builder) SetCurrentDate

func (qb *Builder) SetCurrentDate(field string) *Builder

SetCurrentDate 设置字段为当前日期

func (*Builder) SetElemMatch

func (qb *Builder) SetElemMatch(field string, match bsonV2.M) *Builder

SetElemMatch 设置数组字段的匹配条件

func (*Builder) SetExists

func (qb *Builder) SetExists(field string, exists bool) *Builder

SetExists 设置字段是否存在条件

func (*Builder) SetFilter

func (qb *Builder) SetFilter(filter bsonV2.M) *Builder

SetFilter 设置查询过滤条件

func (*Builder) SetGeoIntersects

func (qb *Builder) SetGeoIntersects(field string, geometry bsonV2.M) *Builder

SetGeoIntersects 设置地理位置相交查询条件

func (*Builder) SetGeoWithin

func (qb *Builder) SetGeoWithin(field string, geometry bsonV2.M) *Builder

SetGeoWithin 设置地理位置范围查询条件

func (*Builder) SetGreaterThan

func (qb *Builder) SetGreaterThan(field string, value interface{}) *Builder

SetGreaterThan 设置字段的大于条件

func (*Builder) SetIn

func (qb *Builder) SetIn(field string, values []interface{}) *Builder

SetIn 设置字段的包含条件

func (*Builder) SetLessThan

func (qb *Builder) SetLessThan(field string, value interface{}) *Builder

SetLessThan 设置字段的小于条件

func (*Builder) SetLimit

func (qb *Builder) SetLimit(limit int64) *Builder

SetLimit 设置查询结果的限制数量

func (*Builder) SetMod

func (qb *Builder) SetMod(field string, divisor, remainder int) *Builder

SetMod 设置字段的模运算条件

func (*Builder) SetNear

func (qb *Builder) SetNear(field string, point bsonV2.M, maxDistance, minDistance float64) *Builder

SetNear 设置地理位置附近查询条件

func (*Builder) SetNearSphere

func (qb *Builder) SetNearSphere(field string, point bsonV2.M, maxDistance, minDistance float64) *Builder

SetNearSphere 设置球面距离附近查询条件

func (*Builder) SetNotEqual

func (qb *Builder) SetNotEqual(field string, value interface{}) *Builder

SetNotEqual 设置字段的不等于条件

func (*Builder) SetNotIn

func (qb *Builder) SetNotIn(field string, values []interface{}) *Builder

SetNotIn 设置字段的排除条件

func (*Builder) SetOr

func (qb *Builder) SetOr(conditions []bsonV2.M) *Builder

SetOr 设置多个条件的逻辑或

func (*Builder) SetPage

func (qb *Builder) SetPage(page, size int64) *Builder

SetPage 设置分页功能,page 从 1 开始,size 为每页的文档数量

func (*Builder) SetProjection

func (qb *Builder) SetProjection(projection bsonV2.M) *Builder

SetProjection 设置查询结果的字段投影

func (*Builder) SetRegex

func (qb *Builder) SetRegex(field string, pattern string, options string) *Builder

SetRegex 设置正则表达式查询条件

func (*Builder) SetSize

func (qb *Builder) SetSize(field string, size int) *Builder

SetSize 设置数组字段的大小条件

func (*Builder) SetSkip

func (qb *Builder) SetSkip(skip int64) *Builder

SetSkip 设置查询结果的跳过数量

func (*Builder) SetSkipLimit

func (qb *Builder) SetSkipLimit(offset, limit int64) *Builder

SetSkipLimit 设置 offset/limit(用于仓库中调用)

func (*Builder) SetSort

func (qb *Builder) SetSort(sort bsonV2.D) *Builder

SetSort 设置查询结果的排序条件

func (*Builder) SetSortWithPriority

func (qb *Builder) SetSortWithPriority(sortFields []bsonV2.E) *Builder

SetSortWithPriority 设置查询结果的排序条件,并指定优先级

func (*Builder) SetTextSearch

func (qb *Builder) SetTextSearch(search string) *Builder

SetTextSearch 设置文本搜索条件

func (*Builder) SetTokenPagination

func (qb *Builder) SetTokenPagination(token string, pageSize int64) *Builder

SetTokenPagination 保存 token 分页信息(供上层解析)

func (*Builder) SetType

func (qb *Builder) SetType(field string, typeValue interface{}) *Builder

SetType 设置字段的类型条件

func (*Builder) Where

func (qb *Builder) Where(cond bsonV2.M) *Builder

Where 将传入的过滤条件合并到 Builder 的 filter 中(覆盖同名字段)

Jump to

Keyboard shortcuts

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