-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...main/kotlin/com/github/inflab/example/spring/data/mongodb/repository/ProjectRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.github.inflab.example.spring.data.mongodb.repository | ||
|
||
import com.github.inflab.spring.data.mongodb.core.aggregation.aggregation | ||
import org.springframework.data.mongodb.core.MongoTemplate | ||
import org.springframework.data.mongodb.core.aggregation.Aggregation.REMOVE | ||
import org.springframework.data.mongodb.core.aggregation.AggregationResults | ||
import org.springframework.data.mongodb.core.aggregation.ComparisonOperators | ||
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class ProjectRepository( | ||
private val mongoTemplate: MongoTemplate, | ||
) { | ||
|
||
data class Author(val first: String, val last: String, val middle: String?) | ||
data class TitleAndAuthorDto( | ||
val id: String, | ||
val title: String, | ||
val author: Author, | ||
) | ||
|
||
/** | ||
* @see <a href="https://www.mongodb.com/docs/manual/reference/operator/aggregation/project/#conditionally-exclude-fields">Conditionally Exclude Fields</a> | ||
*/ | ||
fun excludeConditionally(): AggregationResults<TitleAndAuthorDto> { | ||
val aggregation = aggregation { | ||
project { | ||
+"title" | ||
"author.first" alias "author.first" | ||
"author.last" alias "author.last" | ||
"author.middle" expression ConditionalOperators.`when`( | ||
ComparisonOperators.valueOf("author.middle").equalToValue(""), | ||
).thenValueOf(REMOVE).otherwiseValueOf("author.middle") | ||
} | ||
} | ||
|
||
return mongoTemplate.aggregate(aggregation, BOOKS, TitleAndAuthorDto::class.java) | ||
} | ||
|
||
companion object { | ||
const val BOOKS = "books" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
.../kotlin/com/github/inflab/example/spring/data/mongodb/repository/ProjectRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.github.inflab.example.spring.data.mongodb.repository | ||
|
||
import com.github.inflab.example.spring.data.mongodb.extension.makeMongoTemplate | ||
import io.kotest.core.spec.style.FreeSpec | ||
import io.kotest.matchers.shouldBe | ||
import org.bson.Document | ||
|
||
internal class ProjectRepositoryTest : FreeSpec({ | ||
val mongoTemplate = makeMongoTemplate() | ||
val sortByCountRepository = ProjectRepository(mongoTemplate) | ||
|
||
beforeSpec { | ||
val documents = listOf( | ||
mapOf("_id" to 1, "title" to "abc123", "isbn" to "0001122223334", "author" to mapOf("last" to "zzz", "first" to "aaa"), "copies" to 5, "lastModified" to "2016-07-28"), | ||
mapOf("_id" to 2, "title" to "Baked Goods", "isbn" to "9999999999999", "author" to mapOf("last" to "xyz", "first" to "abc", "middle" to ""), "copies" to 2, "lastModified" to "2017-07-21"), | ||
mapOf("_id" to 3, "title" to "Ice Cream Cakes", "isbn" to "8888888888888", "author" to mapOf("last" to "xyz", "first" to "abc", "middle" to "mmm"), "copies" to 5, "lastModified" to "2017-07-22"), | ||
).map(::Document) | ||
|
||
mongoTemplate.insert(documents, ProjectRepository.BOOKS) | ||
} | ||
|
||
"excludeConditionally" { | ||
// when | ||
val result = sortByCountRepository.excludeConditionally() | ||
|
||
// then | ||
result.mappedResults.map { it.title } shouldBe listOf( | ||
"abc123", | ||
"Baked Goods", | ||
"Ice Cream Cakes", | ||
) | ||
result.mappedResults.map { it.author.middle } shouldBe listOf( | ||
null, | ||
null, | ||
"mmm", | ||
) | ||
} | ||
}) |