使用MongoTemplate保存数据时,抛出异常,Unsupported OP_QUERY command: update
Posted Hi,all
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用MongoTemplate保存数据时,抛出异常,Unsupported OP_QUERY command: update相关的知识,希望对你有一定的参考价值。
目录
1. 环境
Mongodb: docker pull mongo:latest
项目依赖:spring-boot-starter-data-mongodb:2.2.5.RELEASE
2. 使用方法
mongoTemplate.save();
3. 异常信息
[ERROR] [09:41:37] com.c3.base.core.web.controller.ControllerAdvisor - 捕捉到异常:
org.springframework.data.mongodb.UncategorizedMongoDbException: "serverUsed" : "127.0.0.1:27017" , "ok" : 0.0 , "errmsg" : "Unsupported OP_QUERY command: update. The client driver may require an upgrade. For more details see https://dochub.mongodb.org/core/legacy-opcode-removal" , "code" : 352 , "codeName" : "UnsupportedOpQueryCommand"; nested exception is com.mongodb.CommandFailureException: "serverUsed" : "192.168.234.111:27017" , "ok" : 0.0 , "errmsg" : "Unsupported OP_QUERY command: update. The client driver may require an upgrade. For more details see https://dochub.mongodb.org/core/legacy-opcode-removal" , "code" : 352 , "codeName" : "UnsupportedOpQueryCommand"
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:101)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2075)
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:464)
at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:1047)
at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:987)
at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:933)
at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:921)
4. 原因
猜测使用的是 MongoDB 5+ 后端,它有 OP_QUERY explicitly deprecated。尝试降低安装的mongodb版本
docker run -d --name mongo --restart=always -e TZ=Asia/Shanghai -v /home/mongo/data/db:/data/db -v /home/mongo/data/log:/var/log/mongodb -p 27017:27017 --privileged=true mongo:4.4.0
问题得到解决
以上是关于使用MongoTemplate保存数据时,抛出异常,Unsupported OP_QUERY command: update的主要内容,如果未能解决你的问题,请参考以下文章
使用spring boot MongoTemplate删除mongodb记录会抛出错误:“Second is Null”