MongoDB Dao层复杂查询、更新方法实现
来源:互联网 发布:知乎怎么发问题 编辑:程序博客网 时间:2024/06/03 20:44
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之后直接更新countif(!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;}
/** * 更新当天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
- MongoDB Dao层复杂查询、更新方法实现
- mongodb实现复杂报表查询
- 通用型Dao层简单化查询通用方法
- hibernate的dao层实现条件查询
- mongodb复杂查询
- Mongodb复杂查询语句
- 使用morphia实现对mongodb的复杂group查询
- dao层数据库查询操作
- DAO层实现分页的方法.代码作为记录.
- dao实现层通用增删改查方法
- dao层的泛型实现(2种方法)
- java实现遍历树形菜单方法——Dao层
- dao层List<T> 通过泛型实现公共方法 所有dao共用
- DAO层小结2(查询)
- Step into MongoDB - 07 - 复杂查询
- Mongodb与spring集成(3)------MongoRepository实现增删改查和复杂查询
- Mongodb与spring集成(3)------MongoRepository实现增删改查和复杂查询
- Mongodb与spring集成 MongoRepository实现增删改查和复杂查询
- 什么是堡垒机?
- 页面跳转传参:参数过长(cookie,url , AngularJs controller 传参实现)
- java实现算术表达式(+、-、*、/、%、!、^、||、sin、cos、tan、cot、lg、ln等)
- mysql提示Packet for query is too large (1142 > 1024)解决方案
- pyhotn3入门基础-8切片
- MongoDB Dao层复杂查询、更新方法实现
- 16.java语言基础-获取数组最大和最小元素
- shiro注解权限控制-5个权限注解
- eclipse远程debug
- C语言格式符%d与%D的区别
- 使用spring boot admin监控spring cloud应用程序
- 材料科学+网卡=下一代网卡
- 19.java语言基础-元素出现索引(线性搜索)
- [leetcode]142. Linked List Cycle II