clause

package
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: Apache-2.0 Imports: 7 Imported by: 1

Documentation

Overview

Package clause 提供了 MongoDB 查询条件构建的功能,支持各种查询操作符和复杂条件组合。 该包允许用户通过链式调用构建 MongoDB 查询条件,支持等于、大于、小于、IN、NOT 等操作符, 以及 OR、AND、NOT、NOR 等复杂逻辑组合。

Package clause 提供查询条件构建功能,支持SQL风格的查询条件解析 用于构建MongoDB的查询条件,支持多种查询格式和复杂的条件组合

Index

Constants

View Source
const (
	MongoPrimaryName     = "_id" // MongoDB默认主键字段名
	QueryOperationPrefix = "$"   // MongoDB查询操作符前缀
)

定义MongoDB查询相关的常量

View Source
const (
	QueryOperationOR  = "or"  // 逻辑或操作符
	QueryOperationAND = "and" // 逻辑与操作符
	QueryOperationNOT = "not" // 逻辑非操作符
	QueryOperationNOR = "nor" // 逻辑或非操作符
)

定义复杂条件操作符常量

Variables

This section is empty.

Functions

func IsQueryFormat

func IsQueryFormat(s string) bool

IsQueryFormat 判断字符串是否为SQL风格的查询条件格式 检查字符串是否包含任何支持的条件操作符 参数 s: 要检查的字符串 返回值: true表示是SQL风格的查询条件格式

func Multiple

func Multiple(query Filter) bool

Multiple 判断是批量操作还是单个文档操作 通过检查查询条件中主键字段的值类型来判断: - 如果主键字段不存在,默认返回true(批量操作) - 如果主键字段值是map或bson.M类型,返回true(批量操作,如使用$in等条件) - 否则返回false(单个文档操作)

参数 query: 查询条件Filter 返回值: true表示批量操作,false表示单个文档操作

Types

type Filter

type Filter bson.M

Filter 是 bson.M 的别名,用于表示 MongoDB 查询过滤器。 它提供了一系列方法来构建和操作 MongoDB 查询条件。

func (Filter) AND

func (this Filter) AND(v interface{})

AND $and performs a logical AND operation on an array of one or more expressions (e.g. <expression1>, <expression2>, etc.) and selects the documents that satisfy all the expressions in the array. The $and operator uses short-circuit evaluation. If the first expression (e.g. <expression1>) evaluates to false, MongoDB will not evaluate the remaining expressions.

func (Filter) Any

func (this Filter) Any(t, k string, v interface{})

Any 向过滤器添加任意类型的条件。 参数 t 是操作符类型(如 eq, gt, lt 等) 参数 k 是字段名 参数 v 是比较值 如果操作符类型不是以 $ 开头,会自动添加 $ 前缀。 对于需要数组类型参数的操作符(如 $in, $nin),会自动将值转换为数组。

使用示例: filter := clause.Filter{} filter.Any("gt", "age", 18) // 结果: { "age": { "$gt": 18 } } filter.Any("in", "status", []string{"active", "pending"}) // 结果: { "status": { "$in": ["active", "pending"] } }

func (Filter) Eq

func (this Filter) Eq(k string, v interface{})

Eq 添加等于($eq)条件匹配。 参数 k 是字段名 参数 v 是要匹配的值 如果该字段已经存在条件,则自动转换为 $in 操作符。

使用示例: filter := clause.Filter{} filter.Eq("name", "John") // 结果: { "name": "John" } filter.Eq("name", "Jane") // 结果: { "name": { "$in": ["John", "Jane"] } }

func (Filter) Gt

func (this Filter) Gt(k string, v interface{})

Gt 添加大于($gt)条件匹配。 参数 k 是字段名 参数 v 是要比较的值

使用示例: filter := clause.Filter{} filter.Gt("age", 18) // 结果: { "age": { "$gt": 18 } }

func (Filter) Gte

func (this Filter) Gte(k string, v interface{})

Gte 添加大于等于($gte)条件匹配。 参数 k 是字段名 参数 v 是要比较的值

使用示例: filter := clause.Filter{} filter.Gte("age", 18) // 结果: { "age": { "$gte": 18 } }

func (Filter) In

func (this Filter) In(k string, v interface{})

In The $in operator selects the documents where the value of a field equals any value in the specified array

func (Filter) Lt

func (this Filter) Lt(k string, v interface{})

Lt 小于(<)

func (Filter) Lte

func (this Filter) Lte(k string, v interface{})

Lte 小于等于(<=)

func (Filter) Marshal

func (this Filter) Marshal() ([]byte, error)

func (Filter) Match

func (this Filter) Match(t string, v interface{})

Match 添加特殊匹配条件(or, not, and, nor)。 参数 t 是匹配类型(如 or, not, and, nor) 参数 v 是匹配值 如果匹配类型不是以 $ 开头,会自动添加 $ 前缀。

使用示例: filter := clause.Filter{} filter.Match("or", bson.M{"name": "John"}) filter.Match("or", bson.M{"name": "Jane"}) // 结果: { "$or": [{ "name": "John" }, { "name": "Jane" }] }

func (Filter) Merge added in v1.1.0

func (this Filter) Merge(src Filter)

func (Filter) NOR

func (this Filter) NOR(v interface{})

NOR $nor performs a logical NOR operation on an array of one or more query expression and selects the documents that fail all the query expressions in the array.

func (Filter) NOT

func (this Filter) NOT(v interface{})

NOT $not performs a logical NOT operation on the specified <operator-expression> and selects the documents that do not match the <operator-expression>. This includes documents that do not contain the field.

func (Filter) Ne

func (this Filter) Ne(k string, v interface{})

Ne 不等于(!=)

func (Filter) Nin

func (this Filter) Nin(k string, v ...interface{})

Nin selects the documents where: the field value is not in the specified array or the field does not exist.

func (Filter) OR

func (this Filter) OR(v interface{})

OR The $or operator performs a logical OR operation on an array of two or more <expressions> and selects the documents that satisfy at least one of the <expressions>.

func (Filter) Primary

func (this Filter) Primary(v interface{})

Primary 使用主键字段(_id)进行匹配。 参数 v 是要匹配的主键值。

使用示例: filter := clause.Filter{} filter.Primary("5f7d8e9a0b1c2d3e4f5a6b7c") // 结果: { "_id": "5f7d8e9a0b1c2d3e4f5a6b7c" }

func (Filter) String

func (this Filter) String() string

type Node

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

Node 表示单个查询条件节点。 t 是操作符类型(如 $eq, $gt, $lt 等) k 是字段名 v 是比较值

type Query

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

Query 表示 MongoDB 查询条件构建器,用于构建复杂的查询条件。 filter 是查询过滤器 where 是简单条件节点列表 complex 是复杂条件(or, and, not, nor)的节点映射

func New

func New() *Query

New 创建一个新的查询条件构建器实例。 返回值是新的 Query 实例,用于构建 MongoDB 查询条件。

使用示例: query := clause.New() query.Eq("name", "John").Gt("age", 18)

func (*Query) AND

func (q *Query) AND(v ...*Node)

AND 添加 $and 条件匹配,对一个或多个表达式执行逻辑 AND 操作,选择满足所有表达式的文档。 $and 操作符使用短路求值:如果第一个表达式为 false,MongoDB 将不会评估其余表达式。 参数 v 是要进行 AND 操作的条件节点列表

使用示例: query := clause.New() query.AND(

&clause.Node{t: "$gt", k: "age", v: 18},
&clause.Node{t: "$lt", k: "age", v: 30},

) // { "$and": [{ "age": { "$gt": 18 } }, { "age": { "$lt": 30 } }] }

func (*Query) Build

func (q *Query) Build(model *schema.Schema) Filter

Build 生成MongoDB查询条件,支持模型字段映射和复杂条件构建 参数 model: 可选的模型schema,用于字段名映射(结构体字段名到数据库字段名) 返回值: 构建完成的Filter查询条件

使用示例: query := clause.New().Eq("name", "test").Gt("age", 18) filter := query.Build(userSchema) // 结果: { "name": "test", "age": { "$gt": 18 } }

func (*Query) Eq

func (q *Query) Eq(k string, v interface{})

Eq 添加等于($eq)条件匹配。 参数 k 是字段名 参数 v 是要匹配的值

使用示例: query := clause.New() query.Eq("name", "John") // { "name": "John" }

func (*Query) Gt

func (q *Query) Gt(k string, v interface{})

Gt 添加大于($gt)条件匹配。 参数 k 是字段名 参数 v 是要比较的值

使用示例: query := clause.New() query.Gt("age", 18) // { "age": { "$gt": 18 } }

func (*Query) Gte

func (q *Query) Gte(k string, v interface{})

Gte 添加大于等于($gte)条件匹配。 参数 k 是字段名 参数 v 是要比较的值

使用示例: query := clause.New() query.Gte("age", 18) // { "age": { "$gte": 18 } }

func (*Query) In

func (q *Query) In(k string, v interface{})

In 添加 $in 条件匹配,选择字段值等于指定数组中任何值的文档。 参数 k 是字段名 参数 v 是要匹配的数组

使用示例: query := clause.New() query.In("status", []string{"active", "pending"}) // { "status": { "$in": ["active", "pending"] } }

func (*Query) Len

func (q *Query) Len() (r int)

Len 返回查询条件中节点的总数,包括简单条件和复杂条件。 返回值是查询条件中所有节点的数量。

使用示例: query := clause.New() query.Eq("name", "John").Gt("age", 18) fmt.Println(query.Len()) // 输出: 2

func (*Query) Lt

func (q *Query) Lt(k string, v interface{})

Lt 添加小于($lt)条件匹配。 参数 k 是字段名 参数 v 是要比较的值

使用示例: query := clause.New() query.Lt("age", 30) // { "age": { "$lt": 30 } }

func (*Query) Lte

func (q *Query) Lte(k string, v interface{})

Lte 添加小于等于($lte)条件匹配。 参数 k 是字段名 参数 v 是要比较的值

使用示例: query := clause.New() query.Lte("age", 30) // { "age": { "$lte": 30 } }

func (*Query) Marshal

func (q *Query) Marshal() ([]byte, error)

Marshal 将查询条件转换为 BSON 格式的字节数组。 返回值是 BSON 格式的字节数组和可能的错误信息。

使用示例: query := clause.New() query.Eq("name", "John").Gt("age", 18) bsonData, err := query.Marshal()

func (*Query) NOR

func (q *Query) NOR(v ...*Node)

NOR 添加 $nor 条件匹配,对一个或多个查询表达式执行逻辑 NOR 操作,选择不满足所有查询表达式的文档。 参数 v 是要进行 NOR 操作的条件节点列表

使用示例: query := clause.New() query.NOR(

&clause.Node{t: "$eq", k: "status", v: "active"},
&clause.Node{t: "$eq", k: "status", v: "pending"},

) // { "$nor": [{ "status": "active" }, { "status": "pending" }] }

func (*Query) NOT

func (q *Query) NOT(v ...*Node)

NOT 添加 $not 条件匹配,对指定的操作符表达式执行逻辑 NOT 操作,选择不匹配该表达式的文档。 这包括不包含该字段的文档。 参数 v 是要进行 NOT 操作的条件节点列表

使用示例: query := clause.New() query.NOT(

&clause.Node{t: "$eq", k: "status", v: "inactive"},

) // { "$not": [{ "status": "inactive" }] }

func (*Query) Ne

func (q *Query) Ne(k string, v interface{})

Ne 添加不等于($ne)条件匹配。 参数 k 是字段名 参数 v 是要比较的值

使用示例: query := clause.New() query.Ne("name", "John") // { "name": { "$ne": "John" } }

func (*Query) Nin

func (q *Query) Nin(k string, v interface{})

Nin 添加 $nin 条件匹配,选择字段值不在指定数组中或字段不存在的文档。 参数 k 是字段名 参数 v 是要排除的数组

使用示例: query := clause.New() query.Nin("status", []string{"inactive", "deleted"}) // { "status": { "$nin": ["inactive", "deleted"] } }

func (*Query) OR

func (q *Query) OR(v ...*Node)

OR 添加 $or 条件匹配,对两个或多个表达式执行逻辑 OR 操作,选择满足至少一个表达式的文档。 参数 v 是要进行 OR 操作的条件节点列表

使用示例: query := clause.New() query.OR(

&clause.Node{t: "$eq", k: "name", v: "John"},
&clause.Node{t: "$eq", k: "name", v: "Jane"},

) // { "$or": [{ "name": "John" }, { "name": "Jane" }] }

func (*Query) Primary

func (q *Query) Primary(v interface{})

Primary 使用主键字段(_id)进行匹配,可以匹配单个值或值数组。 参数 v 是要匹配的主键值或值数组。

使用示例: query := clause.New() query.Primary("5f7d8e9a0b1c2d3e4f5a6b7c") // 单个主键值 或 query.Primary([]string{"id1", "id2", "id3"}) // 多个主键值

func (*Query) String

func (q *Query) String() string

String 将查询条件转换为 JSON 格式的字符串。 返回值是 JSON 格式的字符串。 如果转换失败,返回空字符串。

使用示例: query := clause.New() query.Eq("name", "John").Gt("age", 18) fmt.Println(query.String()) // 输出: {"name":"John","age":{"$gt":18}}

func (*Query) Where

func (q *Query) Where(format any, cons ...any)

Where 构造查询条件,支持多种格式 支持的格式: 1. SQL风格字符串: Where("name = ? AND age > ?", "John", 18) 2. 主键值: Where("5f8d0d55b54764421b715620") 3. 字段名和值: Where("name", "John") 或 Where("age", []int{18, 20, 22}) 4. Filter类型: Where(Filter{"name": "John"}) 5. map[string]any类型: Where(map[string]any{"name": "John"}) 6. bson.M类型: Where(bson.M{"name": "John"})

参数 format: 查询条件格式 参数 cons: 查询参数

Jump to

Keyboard shortcuts

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