Spring MongoTemplate upsert

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);
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值