MongoDB Dao层复杂查询、更新方法实现

1.饿汉单例模式

	private static final MongoPVPDaoImpl instance = new MongoPVPDaoImpl();// 饿汉式单例模式
	
	public static MongoPVPDaoImpl getInstance(){
		return instance;
	}
	
	private static MongoCollection<Document> recordCollection;
//	private static MongoCollection<Document> dataCollection;
	
	public MongoPVPDaoImpl(){
		MongoDBUtil dbUtil = MongoDBUtil.getInstance();
		recordCollection = dbUtil.getPVPRecordCollection();
//		dataCollection = dbUtil.getPVPDataCollection();
	}

2.插入一条记录,需要先查找该记录所属的国家是否存在,不存在就先创建

/**
	 * 插入当天 pvp record
	 */
	public boolean insertOneRecord(Player player, Player playerMatch, boolean isRobot) {
		String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis());
		boolean result = true;
		try {
			findOneRecord(player.getCountry());
			Document document = new Document();
			document.append("isRobot", isRobot);
			document.append("time", System.currentTimeMillis());
			
			document.append("playerUid", player.getUid());
			document.append("playerName", player.getNickname());
			document.append("playerIp", player.getUser().getIpAddress());
			document.append("playerServer", player.getServerName());
			document.append("playerCountry", player.getCountry());
			
			if(!isRobot){
				document.append("matchUid", playerMatch.getUid());
				document.append("matchName", playerMatch.getNickname());
				document.append("matchIp", playerMatch.getUser().getIpAddress());
				document.append("matchServer", playerMatch.getServerName());
				document.append("matchCountry", playerMatch.getCountry());
			}
			
			BasicDBObject bson = new BasicDBObject("country", player.getCountry());
			BasicDBObject bson2 = new BasicDBObject("$push", new BasicDBObject("record."+dateStr, document));
			updateOneRecord(bson, bson2);
			
			//插入record之后直接更新count
			if(!isRobot){
				updatePVPData(player.getCountry(), "playerMatch");
			}else{
				updatePVPData(player.getCountry(), "robotMatch");
			}
			
		} catch (Exception e) {
			result = false;
		}
		return result;
	}
	
	public boolean insertRecord(Document document){
		boolean result = true;
		try {
			recordCollection.insertOne(document);
		} catch (Exception e) {
			result = false;
		}
		return result;
	}


3.更新一条记录,需要先查找该记录所属的国家是否存在,不存在就先创建

/**
	 * 更新当天pvp data
	 */
	public boolean updatePVPData(String country, String which){
		boolean result = true;
		String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis());
		try {
			Document doc = findOneRecord(country);
			
			Document countDoc = (Document) doc.get("count");
			Document countDateDoc = (Document) countDoc.get(dateStr);
			BasicDBObject bson = new BasicDBObject("country", country);
			long count = countDateDoc.getLong(which);
			BasicDBObject bson2 = new BasicDBObject("$set", new BasicDBObject("count."+dateStr+"."+which, count+1));
			updateOneRecord(bson, bson2);
			
		} catch (Exception e) {
			result = false;
		}	
		return result;
	}
	

4.根据国家查找该国家的记录是否存在,不存在就创建

public Document findOneRecord(String country) {
		
		BasicDBObject bson = new BasicDBObject();
		bson.put("country", country);
		FindIterable<Document> documents = recordCollection.find(bson);
		
		Document document = new Document();
		for(Document doc : documents){
			document = doc;
			break;
		}
		String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis());
		Document document3 = new Document();
		document3.append("reqPVPCount", 0L);
		document3.append("playerMatch", 0L);
		document3.append("robotMatch", 0L);
		document3.append("cancelCount", 0L);
		if(document.size() == 0){
			document.append("country", country);
			document.append("count", new Document().append(dateStr, document3));
			document.append("record", new Document().append(dateStr, new ArrayList<Document>()));
			insertRecord(document);
		}
		Document countDoc = (Document) document.get("count");
		Document countDateDoc = (Document) countDoc.get(dateStr);
		if(countDateDoc == null){
			BasicDBObject bson1 = new BasicDBObject("country", country);
			BasicDBObject bson2 = new BasicDBObject("$set", new BasicDBObject("count."+dateStr, document3));
			updateOneRecord(bson1, bson2);
			
			documents = recordCollection.find(bson);
			for(Document doc : documents){
				document = doc;
				break;
			}
		}
		return document;
	}

5.更新一条记录

public boolean updateOneRecord(BasicDBObject bson1, BasicDBObject bson2){
		boolean result = true;
		try {
			recordCollection.updateOne(bson1, bson2);
		} catch (Exception e) {
			result = false;
		}
		return result;
	}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值