How to get id of document when add custom method to Base Repository in Spring Framework

I have issue when i trying custom base repository, I can’t get id name of document when custom it

In my document field name of id is: _id
But when i using method metadata.idAttribute it return only id

val query = Query((Criteria.where(metadata.idAttribute).`in`(ids)))
val update = Update()
return mongoOperations.updateMulti(query, update, metadata.collectionName).modifiedCount > 0

metadata.idAttribute it should return _id not id

here is full code

class MongoExtendedRepositoryImpl<T, ID : Serializable>(var metadata: MongoEntityInformation<T, ID>, var mongoOperations: MongoOperations)
    : SimpleMongoRepository<T, ID>(metadata, mongoOperations), MongoExtendedRepository<T, ID> {

    override fun setEnableById(enable: Boolean, id: ID): Boolean {
        return setEnableByIds(enable, listOf(id))
    }

    override fun setEnableByIds(enable: Boolean, ids: List<ID>): Boolean {
        //metadata.idAttribute not get exactly data we needed
        val query = Query((Criteria.where("_id").`in`(ids)))
        val update = Update()
        update.set("enabled", enable)
        return mongoOperations.updateMulti(query, update, metadata.collectionName).modifiedCount > 0
    }

    override fun setRemoveById(remove: Boolean, id: ID): Boolean {
        return setRemoveByIds(remove, listOf(id))
    }

    override fun setRemoveByIds(remove: Boolean, ids: List<ID>): Boolean {
        //metadata.idAttribute not get exactly data we needed
        val query = Query((Criteria.where(metadata.idAttribute).`in`(ids)))
        val update = Update()
        update.set("remove", remove)
        return mongoOperations.updateMulti(query, update, metadata.collectionName).modifiedCount > 0
    }


}

Leave a Reply

avatar
  Subscribe  
Notify of