-
Notifications
You must be signed in to change notification settings - Fork 13
Description
使用场景 | Use Case Scenario
While the current query package supports the building of many common query statements, further enhancements are necessary to achieve support for all MongoDB query operators.
尽管当前的 query 包已经支持多种常见的查询语句的构建,但为了实现对所有 MongoDB 查询操作符的支持,需要进一步完善此包。
可行方案 | Feasible Solutions
MongoDB's query operators include Comparison Query Operators, Logical Query Operators, Element Query Operators, Array Query Operators, Evaluation Query Operators, Projection Query Operators, and more (see Query and Projection Operators).
You will need to create corresponding .go files for each category of operators, implement the construction of query statements for the operators in that category, and implement the corresponding methods in the builder.
For example, if you want to implement the construction of the eq query statement for Comparison Query Operators, follow these steps:
- First, add the
Eqfunction inbuilder/query/bson_build.go.func Eq(key string, value any) bson.D { return bson.D{bson.E{Key: key, Value: bson.D{{Key: types.Eq, Value: value}}}} }
- Next, create the corresponding unit tests in
bson_build_test.go. - Then, create or update
comparison_query_builder.goinbuilder/query, adding theEqmethod to thecomparisonQueryBuilderstruct:type comparisonQueryBuilder struct { parent *Builder } func (b *comparisonQueryBuilder) Eq(key string, value any) *Builder { e := bson.E{Key: types.Eq, Value: value} if !b.parent.tryMergeValue(key, e) { b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}}) } return b.parent }
- Finally, add method unit tests in comparison_query_builder_test.go.
MongoDB 的查询操作符包括:比较操作符(Comparison Query Operators)、逻辑操作符(Logical Query Operators)、元素操作符(Element Query Operators)、数组操作符(Array Query Operators)、评估操作符(Evaluation Query Operators)和投影操作符(Projection Query Operators)等(详见 Query and Projection Operators)。
您需要为每类操作符创建对应的 .go 文件,实现该类别包含的操作符的查询语句构建,并在构造器中实现相应的方法。
例如,假设你要实现 比较操作符(Comparison Query Operators)的 eq 查询语句的构建功能,步骤如下:
- 首先,在
builder/query/bson_build.go中添加Eq函数。func Eq(key string, value any) bson.D { return bson.D{bson.E{Key: key, Value: bson.D{{Key: types.Eq, Value: value}}}} }
- 其次,创建对应的单元测试在
bson_build_test.go。 - 接下来,在
builder/query下创建或更新comparison_query_builder.go,添加Eq方法到comparisonQueryBuilder结构体:type comparisonQueryBuilder struct { parent *Builder } func (b *comparisonQueryBuilder) Eq(key string, value any) *Builder { e := bson.E{Key: types.Eq, Value: value} if !b.parent.tryMergeValue(key, e) { b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}}) } return b.parent }
- 最后,添加方法的单元测试在
comparison_query_builder_test.go。
其它 | Others
Due to the large number of query operators in MongoDB, it is advisable to implement these features in batches to ensure manageability and reviewability. Each PR should include implementations of multiple operators, rather than implementing one operator per PR (except in special cases).
由于 MongoDB 的查询操作符有很多个,建议分批实现这些功能以确保管理和审查的可行性。每次提交的 PR 应包括多个操作符的实现,避免一个 PR 实现一个操作符(特殊情况除外)。