Spring MongoTemplate upsert 插入未报异常,但是数据库中没有查询到相关记录。
如下,一般为update.set()方法,没有把除了Criteria中的字段都update。
package com.oasis.model;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.CompoundIndexes;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "guanShuTaskInfo")
@CompoundIndexes({
@CompoundIndex(name="guanShuTaskInfo_taskId_index",def="{'taskId':1}", unique=true),
@CompoundIndex(name="guanShuTaskInfo_index",def="{pid':1}")
})
public class TaskBean {
private String taskId;
private String account;
private String name;
private String phoneno;
private String idcard;
private String password;
private String usergid;
private String token;
private String pid;
private String sort;
private int accType;
private int status;
private int ecode;
private int httpCode;
private long createTime;
private long updateTime;
// set get 方法
}
upsert方法
public void saveForTask(TaskBean taskBean) {
logger.debug("saveForTask.taskBean=" + taskBean);
if (null == taskBean) {
logger.debug("saveForTask taskBean is null | return for not operate");
return;
}
Criteria criteria = Criteria.where("taskId").is(taskBean.getTaskId());
Query query = new Query(criteria);
Update update = new Update();
update.set("account", taskBean.getAccount());
update.set("name", taskBean.getName());
// 如果少了一个则会出现插入失败的情况。且不会报异常
update.set("phoneno", taskBean.getPhoneno());
update.set("idcard", taskBean.getIdcard());
update.set("password", taskBean.getPassword());
update.set("usergid", taskBean.getUsergid());
update.set("pid", taskBean.getPid());
update.set("accType", taskBean.getAccType());
update.set("sort", taskBean.getSort());
update.set("status", taskBean.getStatus());
update.set("ecode", taskBean.getEcode());
update.set("httpCode", taskBean.getHttpCode());
update.set("createTime", taskBean.getCreateTime());
update.set("updateTime", taskBean.getUpdateTime());
logger.debug("upsert.taskBean=" + taskBean);
mongoTemplate.insert(taskBean);
logger.debug("upsert.taskId:{} result:{}", taskBean, result);
}