coperator/README.md

69 lines
1.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# COperator
实现MongoDB风格查询操作符的文档过滤库提供灵活的条件匹配功能
## 安装
```bash
# 确保已安装Go环境1.23.1+
go get git.kingecg.top/kingecg/coperator
```
## 核心功能
- **Document过滤**:通过`DocumentOperator`检查文档是否满足过滤条件
- **支持操作符**`$gt`、`$ge`、`$lt`、`$le`、`$eq`、`$ne`、`$in`、`$nin`、`$and`、`$or`、`$nor`
- **嵌套查询**:支持嵌套文档和数组的路径访问(`.`分隔路径)
- **链式条件**:支持`$and`、`$or`等组合条件查询
## 使用示例
```go
package main
import (
"fmt"
"git.kingecg.top/kingecg/coperator"
)
func main() {
// 创建测试文档
doc := &coperator.Document{
"name": "Alice",
"age": 30,
"address": map[string]interface{}{
"city": "Beijing",
"zip": 100000,
},
}
// 定义过滤条件
filter := coperator.Filter{
"age": map[string]interface{}{
"$gt": 25,
},
"address.city": "Beijing",
}
// 执行查询
result, err := coperator.DocumentOperator(doc, filter, "", nil)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("Document matches filter: %v\n", result) // 输出: Document matches filter: true
}
```
## 测试
```bash
# 运行单元测试
go test -v ./...
```
## 贡献
欢迎提交PR或Issue
1. Fork仓库
2. 创建新分支
3. 提交代码
4. 创建Pull Request
## 许可证
MIT License