Skip to content

Commit 80438b7

Browse files
authored
GODRIVER-4030: Implement comparison and element query operators and tests (#17)
* GODRIVER-4030: Implement comparison and element query operators and tests. * Change all instances of implicit field matching to explicit using query.Eq.
1 parent 40d6312 commit 80438b7

2 files changed

Lines changed: 368 additions & 7 deletions

File tree

mql/query/query.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,26 @@ func Field(name string, conds ...FieldCondition) Filter {
3131
return Filter{{Key: name, Value: merged}}
3232
}
3333

34+
// And creates a Filter for logical AND: { $and: [ filter1, filter2, ... ] }.
35+
func And(filters ...Filter) Filter {
36+
clauses := make(bson.A, 0, len(filters))
37+
for _, f := range filters {
38+
clauses = append(clauses, bson.D(f))
39+
}
40+
return Filter{{Key: "$and", Value: clauses}}
41+
}
42+
3443
// Eq creates a FieldCondition for equality: { $eq: value }.
3544
func Eq(value any) FieldCondition {
3645
return FieldCondition{doc: bson.D{{Key: "$eq", Value: value}}}
3746
}
3847

48+
// Exists creates a FieldCondition matching documents that have (or lack) the
49+
// field: { $exists: exists }.
50+
func Exists(exists bool) FieldCondition {
51+
return FieldCondition{doc: bson.D{{Key: "$exists", Value: exists}}}
52+
}
53+
3954
// Gt creates a FieldCondition for greater than: { $gt: value }.
4055
func Gt(value any) FieldCondition {
4156
return FieldCondition{doc: bson.D{{Key: "$gt", Value: value}}}
@@ -46,6 +61,11 @@ func Gte(value any) FieldCondition {
4661
return FieldCondition{doc: bson.D{{Key: "$gte", Value: value}}}
4762
}
4863

64+
// In creates a FieldCondition matching any of the given values: { $in: [ ... ] }.
65+
func In(values ...any) FieldCondition {
66+
return FieldCondition{doc: bson.D{{Key: "$in", Value: bson.A(values)}}}
67+
}
68+
4969
// Lt creates a FieldCondition for less than: { $lt: value }.
5070
func Lt(value any) FieldCondition {
5171
return FieldCondition{doc: bson.D{{Key: "$lt", Value: value}}}
@@ -56,13 +76,21 @@ func Lte(value any) FieldCondition {
5676
return FieldCondition{doc: bson.D{{Key: "$lte", Value: value}}}
5777
}
5878

59-
// And creates a Filter for logical AND: { $and: [ filter1, filter2, ... ] }.
60-
func And(filters ...Filter) Filter {
61-
clauses := make(bson.A, 0, len(filters))
62-
for _, f := range filters {
63-
clauses = append(clauses, bson.D(f))
64-
}
65-
return Filter{{Key: "$and", Value: clauses}}
79+
// Ne creates a FieldCondition matching values not equal to value: { $ne: value }.
80+
func Ne(value any) FieldCondition {
81+
return FieldCondition{doc: bson.D{{Key: "$ne", Value: value}}}
82+
}
83+
84+
// Nin creates a FieldCondition matching none of the given values: { $nin: [ ... ] }.
85+
func Nin(values ...any) FieldCondition {
86+
return FieldCondition{doc: bson.D{{Key: "$nin", Value: bson.A(values)}}}
87+
}
88+
89+
// Type creates a FieldCondition matching documents where the field is one of the
90+
// specified BSON types: { $type: [ ... ] }. Each type may be an alias string or
91+
// numeric code. The verbose array form is always emitted.
92+
func Type(types ...any) FieldCondition {
93+
return FieldCondition{doc: bson.D{{Key: "$type", Value: bson.A(types)}}}
6694
}
6795

6896
// Or creates a Filter for logical OR: { $or: [ filter1, filter2, ... ] }.

0 commit comments

Comments
 (0)