Spring Boot + Mongo-com.mongodb.BasicDocument,您不能添加第二个“ id”条件

时间:2019-10-28 07:50:35

标签: spring mongodb spring-boot

为什么在查询时会出现此错误?

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
)

谢谢

1 个答案:

答案 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)

但不能两者都