DEV Community

qiudaozhang
qiudaozhang

Posted on

3 2

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 进行判断即可

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay