Skip to content

Commit 2fb721c

Browse files
author
Jens Suhr
committed
JAVA-5775 Filter null values in Filters.and / Filters.or
1 parent 0e25654 commit 2fb721c

File tree

2 files changed

+16
-2
lines changed
  • driver-kotlin-extensions/src

2 files changed

+16
-2
lines changed

driver-kotlin-extensions/src/main/kotlin/com/mongodb/kotlin/client/model/Filters.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public object Filters {
288288
* @param filters the list of filters to and together
289289
* @return the filter
290290
*/
291-
public fun and(filters: Iterable<Bson?>): Bson = Filters.and(filters)
291+
public fun and(filters: Iterable<Bson?>): Bson = Filters.and(filters.filterNotNull())
292292

293293
/**
294294
* Creates a filter that performs a logical AND of the provided list of filters. Note that this will only generate
@@ -309,7 +309,7 @@ public object Filters {
309309
* @param filters the list of filters to and together
310310
* @return the filter
311311
*/
312-
public fun or(filters: Iterable<Bson?>): Bson = Filters.or(filters)
312+
public fun or(filters: Iterable<Bson?>): Bson = Filters.or(filters.filterNotNull())
313313

314314
/**
315315
* Creates a filter that preforms a logical OR of the provided list of filters.

driver-kotlin-extensions/src/test/kotlin/com/mongodb/kotlin/client/model/FiltersTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,18 @@ class FiltersTest {
159159
@Test
160160
fun testOrSupport() {
161161
val expected = BsonDocument.parse("""{${'$'}or: [{"name": "Ada"}, {"age": 20 }]}""")
162+
162163
val bson = or(eq(Person::name, person.name), eq(Person::age, person.age))
163164
assertEquals(expected, bson.document)
164165

165166
val kmongoDsl = or(Person::name eq person.name, Person::age eq person.age)
166167
assertEquals(expected, kmongoDsl.document)
168+
169+
val bsonWithNull = or(eq(Person::name, person.name), eq(Person::age, person.age), null)
170+
assertEquals(expected, bsonWithNull.document)
171+
172+
val kmongoDslWithNull = or(Person::name eq person.name, Person::age eq person.age, null)
173+
assertEquals(expected, kmongoDslWithNull.document)
167174
}
168175

169176
@Test
@@ -186,11 +193,18 @@ class FiltersTest {
186193
@Test
187194
fun testAndSupport() {
188195
val expected = BsonDocument.parse("""{${'$'}and: [{"name": "Ada"}, {"age": 20 }]}""")
196+
189197
val bson = and(eq(Person::name, person.name), eq(Person::age, person.age))
190198
assertEquals(expected, bson.document)
191199

192200
val kmongoDsl = and(Person::name.eq(person.name), Person::age.eq(person.age))
193201
assertEquals(expected, kmongoDsl.document)
202+
203+
val bsonWithNull = and(eq(Person::name, person.name), eq(Person::age, person.age), null)
204+
assertEquals(expected, bsonWithNull.document)
205+
206+
val kmongoDslWithNull = and(Person::name.eq(person.name), Person::age.eq(person.age), null)
207+
assertEquals(expected, kmongoDslWithNull.document)
194208
}
195209

196210
@Test

0 commit comments

Comments
 (0)