为什么在查询时会出现此错误?
org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDocument, you can't add a second 'id' criteria. Query already contains '{ "id" : "123"}'
我正在使用Spring Boot和Mongo:
fun subGenreNames(subGenreIds: List<String>?): List<String> {
val results = mutableListOf<String>()
var query = Query()
subGenreIds!!.forEach{
query.addCriteria(Criteria.where("id").`is`(it))
var subGenreName = mongoTemplate.findById(it, SubGenre::class.java)
results.add(subGenreName!!.name)
}
return results
}
我将SubGenre类设置为:
@Document(collection = "subgenres")
data class SubGenre(
@Field("id")
val id: String,
val name: String
)
谢谢
答案 0 :(得分:1)
根据您的代码,您需要使用其中一个
query.addCriteria(Criteria.where("id").`is`(it))
var subGenreName = mongoTemplate.find(query, SubGenre::class.java)
或
var subGenreName = mongoTemplate.findById(it, SubGenre::class.java)
但不能两者都