新增數據:java
/** * save ApproveMongoDto */ public void saveApproveTarget(ApproveMongoDto approveMongoDto) { mongoSquareTemplate.insert(approveMongoDto, APPROVED); }
刪除數據:app
/** * delete ApproveMongoDto * @param d */ public void deleteApproveTarget(ApproveMongoDto d) { mongoSquareTemplate.remove(new Query(Criteria.where("targetId").is(d.getTargetId()).andOperator( Criteria.where("targetType").is(d.getTargetType()))), ApproveMongoDto.class,APPROVED); }
更新數據:dom
/** * update ApproveMongoDto * 曝光+1更新 */ public void updateTargetForShow(Long targetId,Integer targetType,Long uid) { mongoSquareTemplate.updateFirst(new Query(Criteria.where("targetId").is(targetId) .andOperator(Criteria.where("targetType").is(targetType))), new Update().addToSet("uids",uid) .inc("showCount", 1) .set("updateTime",System.currentTimeMillis()) , ApproveMongoDto.class, APPROVED); }
查詢列表:ui
/** * search pageList * @param baseObj * @return */ public List<DBObject> getDBObjectPageList(BasicDBObject baseObj) { DBCollection collection = mongoSquareTemplate.getCollection(APPROVED); Cursor cursor = collection.find(baseObj).limit(20); List<DBObject> list = new ArrayList<DBObject>(); while(cursor.hasNext()){ DBObject obj = cursor.next(); list.add(obj); } return list; }
多條件查詢:code
//查詢mongo-hot數據 BasicDBObject obj1 = new BasicDBObject("state",ContentApproveEnum.approved_new.type); BasicDBObject obj2 = new BasicDBObject("approveCount",new BasicDBObject("$gt",2)); BasicDBObject obj3 = new BasicDBObject("showCount",new BasicDBObject("$lt",6)); Long[] uidArray = {Long.parseLong(uid)}; BasicDBObject obj4 = new BasicDBObject("uids",new BasicDBObject("$nin",uidArray)); BasicDBObject andObj = new BasicDBObject("$and", Arrays.asList(obj3,obj2,obj1,obj4)); List<DBObject> list = contentApproveMongoDao.getDBObjectPageList(andObj); if(list!=null&&list.size()>0){ int index = RandomUtils.randomInt(list.size()); DBObject tempObj = list.get(index); ApproveMongoDto d = new ApproveMongoDto(); d = MongoToBeanUtil.dbObject2Bean(tempObj,d); return d; }else{ return null; }
Mongo索引索引
db.approved.ensureIndex({"targetId":1,"targetType":1},{"unique":true})
db.approved.createIndex({"showCount":1,"state":1,"approvedCount":1})
db.approved.createIndex({"takeCount":1,"state":1})
db.approved.createIndex({"createTime":1,"state":1})rem
db.approved_history.createIndex({"updateTime":1})
db.approved_history.createIndex({"targetId":1,"targetType":1},{"unique":true})get
db.approved_record.createIndex({"createTime":1})it