Skip to content

Commit aa9e66d

Browse files
committed
DATAMONGO-2138 - Infix or/nor/and operators
1 parent 66fa295 commit aa9e66d

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/TypedCriteria.kt

+30
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,16 @@ infix fun KProperty<*>.bits(bitwiseCriteria: Criteria.BitwiseCriteriaOperators.(
429429
*/
430430
fun orOperator(vararg builders: TypedCriteria) = addOperatorWithCriteria(builders, Criteria::orOperator)
431431

432+
/**
433+
* Creates an 'or' criteria using the $or operator for all of the provided criteria
434+
*
435+
* Note that mongodb doesn't support an $or operator to be wrapped in a $not operator.
436+
* @author Tjeu Kayim
437+
* @since 2.2
438+
* @see Criteria.orOperator
439+
*/
440+
infix fun TypedCriteria.or(other: TypedCriteria) = orOperator(this, other)
441+
432442
/**
433443
* Creates a 'nor' criteria using the $nor operator for all of the provided criteria.
434444
*
@@ -439,6 +449,16 @@ fun orOperator(vararg builders: TypedCriteria) = addOperatorWithCriteria(builder
439449
*/
440450
fun norOperator(vararg builders: TypedCriteria) = addOperatorWithCriteria(builders, Criteria::norOperator)
441451

452+
/**
453+
* Creates a 'nor' criteria using the $nor operator for all of the provided criteria.
454+
*
455+
* Note that mongodb doesn't support an $nor operator to be wrapped in a $not operator.
456+
* @author Tjeu Kayim
457+
* @since 2.2
458+
* @see Criteria.norOperator
459+
*/
460+
infix fun TypedCriteria.nor(other: TypedCriteria) = norOperator(this, other)
461+
442462
/**
443463
* Creates an 'and' criteria using the $and operator for all of the provided criteria.
444464
*
@@ -449,6 +469,16 @@ fun norOperator(vararg builders: TypedCriteria) = addOperatorWithCriteria(builde
449469
*/
450470
fun andOperator(vararg builders: TypedCriteria) = addOperatorWithCriteria(builders, Criteria::andOperator)
451471

472+
/**
473+
* Creates an 'and' criteria using the $and operator for all of the provided criteria.
474+
*
475+
* Note that mongodb doesn't support an $and operator to be wrapped in a $not operator.
476+
* @author Tjeu Kayim
477+
* @since 2.2
478+
* @see Criteria.andOperator
479+
*/
480+
infix fun TypedCriteria.and(other: TypedCriteria) = andOperator(this, other)
481+
452482
private fun addOperatorWithCriteria(
453483
builders: Array<out TypedCriteria>,
454484
operation: Criteria.(Array<Criteria>) -> Criteria

spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/TypedCriteriaExtensions.kt

+2-5
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,13 @@ private fun typedCriteriaSample(mongoOperations: MongoOperations) {
5353
// Chain with typedCriteria()
5454
typedCriteria(
5555
Book::author elemMatch
56-
(Author::name isEqualTo "Herman Melville"),
56+
(Author::name isEqualTo "Herman Melville"),
5757
Book::price exists true
5858
)
5959
// $or, $nor, $and operators
6060
typedCriteria(
6161
Book::name isEqualTo "Moby-Dick",
62-
orOperator(
63-
Book::price lt 1200,
64-
Book::price gt 240
65-
)
62+
(Book::price lt 1200) or (Book::price gt 240)
6663
)
6764
// Nested Properties (i.e. refer to "book.author")
6865
typedCriteria(

spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/query/TypedCriteriaExtensionsTests.kt

+32
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,38 @@ class TypedCriteriaExtensionsTests {
416416
assertEqualCriteria(typed, classic)
417417
}
418418

419+
@Test
420+
fun `infix or & and typed criteria should equal classic criteria`() {
421+
422+
val typed = (Book::title isEqualTo "Moby-Dick") and ((Book::price lt 1200) or (Book::price gt 240))
423+
val classic = Criteria().andOperator(
424+
Criteria("title").isEqualTo("Moby-Dick"),
425+
Criteria().orOperator(Criteria("price").lt(1200), Criteria("price").gt(240))
426+
)
427+
println(classic.criteriaObject.toJson())
428+
assertEqualCriteria(typed, classic)
429+
}
430+
431+
@Test
432+
fun `infix or typed criteria should equal classic criteria`() {
433+
434+
val typed = typedCriteria(
435+
Book::title isEqualTo "Moby-Dick",
436+
(Book::price lt 1200) or (Book::price gt 240)
437+
)
438+
val classic = Criteria("title").isEqualTo("Moby-Dick")
439+
.orOperator(Criteria("price").lt(1200), Criteria("price").gt(240));
440+
assertEqualCriteria(typed, classic)
441+
}
442+
443+
@Test
444+
fun `infix nor typed criteria should equal classic criteria`() {
445+
446+
val typed = (Book::price lt 1200) nor (Book::price gt 240)
447+
val classic = Criteria().norOperator(Criteria("price").lt(1200), Criteria("price").gt(240))
448+
assertEqualCriteria(typed, classic)
449+
}
450+
419451
@Test
420452
fun `One level nested typed criteria should equal classic criteria`() {
421453

0 commit comments

Comments
 (0)