【发布时间】:2018-01-19 21:06:05
【问题描述】:
我有一个 Kotlin 函数,它使用如下所示的哈希图创建模型
@GetMapping("/")
fun index(model: Model): Mono<String> {
model.addAttribute("images", imageService.findAllImages()?.flatMap { image ->
Mono.just(image)
.zipWith(repository.findByImageId(image?.id!!).collectList())
.map({ imageAndComments: Tuple2<Image?, MutableList<learningspringboot.images.Comment>> ->
hashMapOf<String, Any?>(
"id" to imageAndComments.t1?.id,
"name" to imageAndComments.t1?.name,
"comments" to imageAndComments.t2)
}).log("findAllImages")
})
model.addAttribute("extra", "DevTools can also detech code changes.")
return Mono.just("index")
}
Image.kt
package learningspringboot.images
import org.springframework.data.annotation.Id
data class Image(@Id var id: String? = null, var name: String? = null)
Comment.kt
package learningspringboot.images
import org.springframework.data.annotation.Id
data class Comment @JvmOverloads constructor(@Id private var id: String? = null, private var imageId: String? = null, private var comment: String? = null) {
}
在我的 Thymeleaf 模板中有
<ul><li th:each = "Comment :${image.comments}" th:text = "${image.comments}"></li></ul>
这给了我这样的线条
[评论(id=5a623d5d2298352bc4929866,imageId=0d46b575-b6ce-48e2-988a-ebe62ebc2ceb,comment=test),评论(id=5a623d8b2298352bc4929867,commentId=0d46b575-b6ce-48ec2-98)
显示评论记录与 MongoDB 键/ID 和其他所有内容一样。这不是我想要的。 我的 Thymeleaf 模板中也有这个
<ul><li th:each = "Comment :${image.comments}" th:text = "${comment == null ? 'empty' : comment.Comment}"></li></ul>
其中显示每个评论记录的单词empty。
数据库中的评论记录是这样的
{ "_id" : ObjectId("5a623d5d2298352bc4929866"), "imageId" : "0d46b575-b6ce-48e2-988a-ebe62ebc2ceb", "comment" : "test", "_class" : "learningspringboot.comments.Comment" }
数据库中的图像记录是这样的
{ "_id" : ObjectId("5a623d5d2298352bc4929866"), "imageId" : "0d46b575-b6ce-48e2-988a-ebe62ebc2ceb", "comment" : "test", "_class" : "learningspringboot.comments.Comment" }
{ "_id" : ObjectId("5a623d8b2298352bc4929867"), "imageId" : "0d46b575-b6ce-48e2-988a-ebe62ebc2ceb", "comment" : "test23", "_class" : "learningspringboot.comments.Comment" }
如何解开 cmets 记录以便只看到“comment”值而不是“_id”或“imageId”值?
【问题讨论】:
标签: spring mongodb spring-boot thymeleaf