mongo:Criteria和Query

[java] view plain copy
  1. Query query=new Query();  
  2. Criteria criteria=Criteria.where("name").is("tom").and("studentNum").in(numberList);  
  3. query.addCriteria(criteria);  
[java] view plain copy
  1. new Query(criteria);  
[java] view plain copy
  1. Criteria criteria=Criteria.where("indexWarnGrade").ne("0").orOperator(Criteria.where("resId").is(channelId),Criteria.where("resBelongChannelId").is(channelId));  
  2. query.addCriteria(criteria);  
  3. query.with(new Sort(Direction.DESC, "indexWarnLatelyDate"));  
[java] view plain copy
  1. //分页  
  2. int currentPage = (initPagingBean.getStart()/initPagingBean.getPageSize())+1;  
  3. Query query=new Query();  
  4.         //查询通道和通道下属的设备告警信息  
  5.         Criteria criteria=Criteria.where("indexWarnGrade").ne("0").orOperator(Criteria.where("resId").is(channelId),Criteria.where("resBelongChannelId").is(channelId));  
  6.         query.addCriteria(criteria);  
  7.         query.with(new Sort(Direction.DESC, "indexWarnLatelyDate"));  
  8.         int total = (intthis.realMongodbServiceImpl.findCount(MonitorObject.class, query);  
  9.         initPagingBean.setTotalItems(total);  
  10.         //查询mo集合  
  11.         List moList=this.realMongodbServiceImpl.findList(MonitorObject.class, query, currentPage, initPagingBean.getPageSize());         
  12.   
  13. initPagingBean.setDataList(wvList);  
  14. return  initPagingBean; 


	@Override
	public void aggregateSelectHotList() {

		String date = "2017-05-20 09:47:22";
		/*
		 * 需要mongo版本2.2以上
		 */
		// 根据label主键分组
		String groupStr = "{$group:{_id:{'dazzleId':'$dazzleId'},totalNum:{$sum:1}}}";
		DBObject group = (DBObject) com.mongodb.util.JSON.parse(groupStr);
		// 取几条数据
		String limitStr = "{$limit:3}";
		DBObject limit = (DBObject) com.mongodb.util.JSON.parse(limitStr);
		// 倒叙排列
		String sortStr = "{$sort:{totalNum:-1}}";
		DBObject sort = (DBObject) com.mongodb.util.JSON.parse(sortStr);

		// 查询条件
		String matchStr = "{$match:{praiseTime:{$gte:\"" + date + "\"}}}";
		DBObject match = (DBObject) com.mongodb.util.JSON.parse(matchStr);
		// 参数有顺序的(根据你的参数顺序进行操作),先查询条件,再分组,再排序,最后取值
		AggregationOutput aggregate = getMongoTemplate().getCollection("praiseRecord").aggregate(match, group, sort,
				limit);
		System.out.println(JSON.toJSONString(aggregate.results()));

	}

	

	/**
	 * 
	 */
	public void groupByTest() {
		String date = "2017-05-20 09:47:22";
		GroupBy groupBy = GroupBy.key("dazzleId").initialDocument("{count:0}")
				.reduceFunction("function(doc, out){out.count++}").finalizeFunction("function(out){return out;}");
		Criteria criteria = Criteria.where("praiseTime").gte(date);
		GroupByResults res = mongoTemplate.group(criteria, "praiseRecord", groupBy, PraiseRecord.class);
		DBObject obj = res.getRawResults();
		System.out.println(JSON.toJSON(obj));

	}

	/**
	 * 查询点赞记录列表
	 */
	@Override
	public Map queryPraiseDazzleList(Map map) {
		// 返回map
		Map returnMap = new HashMap();
		// 分页条件
		Pagination pagination = (Pagination) map.get("pagination");

		// 用户主键
		Integer userId = (Integer) map.get("userId");

		Query query = new Query();
		// 
		Criteria queryCri = Criteria.where("userId").is(userId);
		query.addCriteria(queryCri);
		// 获取满足条件的总条数
		long totalCount = getMongoTemplate().count(query, PraiseRecord.class);
		// 从哪个位置开始取值
		int skip = pagination.getPage();
		// 根据条件倒叙取值
		query.with(new Sort(new Order(Direction.DESC, "praiseTime")));
		query.skip(skip);// skip相当于从那条记录开始
		query.limit(pagination.getRows());// 从skip开始,取多少条记录
		// 获取列表
		List praiseRecordList = getMongoTemplate().find(query, PraiseRecord.class);

		returnMap.put("total", totalCount);
		returnMap.put("praiseRecordList", praiseRecordList);
		return returnMap;
	}

	/**
	 * 修改点赞我的记录都被我已读
	 */
	@Override
	public void updateNewPraiseByDazzleUserId(Integer userId) {
		getMongoTemplate().updateMulti(new Query(Criteria.where("userId").is(userId)),
				new Update().set("newPraise", 0), PraiseRecord.class);

	}






[java] view plain copy
  1. //is相当于等于  
  2. //in相当于sql中的in  
  3. //ne相当于不等于  
  4. //orOperator接受多个条件,组成or逻辑 

你可能感兴趣的:(mongodb)