mongotemplate 进行unset操作

qiudaozhang

qiudaozhang

Posted on June 15, 2022

mongotemplate 进行unset操作

背景

需要将某个数据的某个字段移除,并非设置为null,或者空字符串等 。

mongo shell操作可以如下

db.集合名称.updateOne({id:"xxx"},{
    $unset: {"属性":"" }
})
Enter fullscreen mode Exit fullscreen mode

翻译成kotlin的写法如下

override fun unsetPicture(id: String) {
    val query = Query()
    query.addCriteria(Criteria.where("id").`is`(id))
    val update = Update()
    update.unset("picture")
    mongoTemplate.updateFirst(query, update, clz)
}
Enter fullscreen mode Exit fullscreen mode

如果没有存储id,使用_id 进行判断即可

💖 💪 🙅 🚩
qiudaozhang
qiudaozhang

Posted on June 15, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related