Skip to content

完善 query 包 | enhancement query package #28

@chenmingyong0423

Description

@chenmingyong0423

使用场景 | 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 Eq function in builder/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.go in builder/query, adding the Eq method to the comparisonQueryBuilder struct:
    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).

Contribution Guide


由于 MongoDB 的查询操作符有很多个,建议分批实现这些功能以确保管理和审查的可行性。每次提交的 PR 应包括多个操作符的实现,避免一个 PR 实现一个操作符(特殊情况除外)。

贡献指南

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions