Skip to content

Commit 7157333

Browse files
committed
Render Filters.and as {$and : [] } if the list of filters is empty. That way, the driver leaves it to the server to decide the meaning of an empty array for both $and and $or operators, which currently is to return an error.
JAVA-1828
1 parent 2f46829 commit 7157333

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Diff for: driver-core/src/main/com/mongodb/client/model/Filters.java

+4
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,10 @@ public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentCl
550550
}
551551
}
552552

553+
if (andRenderable.isEmpty()) {
554+
andRenderable.append("$and", new BsonArray());
555+
}
556+
553557
return andRenderable;
554558
}
555559

Diff for: driver-core/src/test/unit/com/mongodb/client/model/FiltersSpecification.groovy

+12
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,24 @@ class FiltersSpecification extends Specification {
116116
toBson(exists('x', false)) == parse('{x : {$exists : false} }')
117117
}
118118

119+
def 'or should render empty or using $or'() {
120+
expect:
121+
toBson(or([])) == parse('{$or : []}')
122+
toBson(or()) == parse('{$or : []}')
123+
}
124+
119125
def 'should render $or'() {
120126
expect:
121127
toBson(or([eq('x', 1), eq('y', 2)])) == parse('{$or : [{x : 1}, {y : 2}]}')
122128
toBson(or(eq('x', 1), eq('y', 2))) == parse('{$or : [{x : 1}, {y : 2}]}')
123129
}
124130

131+
def 'and should render empty and using $and'() {
132+
expect:
133+
toBson(and([])) == parse('{$and : []}')
134+
toBson(and()) == parse('{$and : []}')
135+
}
136+
125137
def 'and should render and without using $and'() {
126138
expect:
127139
toBson(and([eq('x', 1), eq('y', 2)])) == parse('{x : 1, y : 2}')

0 commit comments

Comments
 (0)