Spring-data-mongodb 创建或更新MongoDB数据

本文详细阐述了在特定数据库场景下,通过使用MongoDB模板进行实体信息的更新与插入操作,包括设置属性、条件查询及日志记录,旨在提高数据库操作的效率与准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@Override
public void modifyOrCreateLocationInfo(Query query, Update update) throws DataAccessException {
	this.mongoTemplate.upsert(query, update, LocationInfo.class);
}
	
@Override
public void modifyOrCreateLocationInfo(LocationInfo locationInfo) throws DataAccessException {
    	Update update = new Update();
    		
    	Assert.hasText(locationInfo.getPushChannelId(), "用户App的channelId为空");
    	Assert.notNull(locationInfo.getActor(), "修改地理位置上传参数角色为空");
    	update.set("pushChannelId", locationInfo.getPushChannelId());
    		
    	if (!Strings.isNullOrEmpty(locationInfo.getUserId())) {
    		update.set("userId", locationInfo.getUserId());
    	}
    	if (!Strings.isNullOrEmpty(locationInfo.getPushUserId())) {
    		update.set("pushUserId", locationInfo.getPushUserId());
    	}
    	if (null != locationInfo.getCoordinate()) {
    		update.set("coordinate", locationInfo.getCoordinate());
    	}
    	if (null != locationInfo.getActor()) {
    		update.set("actor", locationInfo.getActor());
    	}
    	if (null != locationInfo.getUserStatus()) {
    		update.set("userStatus", locationInfo.getUserStatus());
    	}
    	if (null != locationInfo.getOnlineState()) {
    		update.set("onlineState", locationInfo.getOnlineState());
    	}
    	if (null == locationInfo.getDistance()) {
    		update.setOnInsert("distance", 50);
    	} else {
    		update.set("distance", locationInfo.getDistance());
    	}
    	if (null != locationInfo.getDeviceType()) {
    		update.set("deviceType", locationInfo.getDeviceType());
    	}
    		
    	Criteria criteria = Criteria.where("pushChannelId")
    			.is(locationInfo.getPushChannelId())
    			.and("actor").is(locationInfo.getActor());
    		
    	Query query = new Query(criteria);
    	logger.debug("增加或修改一条位置记录到数据库 {} {}", query.toString(), update.toString());
    	this.modifyOrCreateLocationInfo(query, update);
}


转载于:https://my.oschina.net/linwenbin/blog/420062

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值