Java Code Examples for org.springframework.data.mongodb.core.MongoTemplate#find()
The following examples show how to use
org.springframework.data.mongodb.core.MongoTemplate#find() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Mongo.java From sagacity-sqltoy with Apache License 2.0 | 5 votes |
/** * @todo 分页查询 * @param mongoTemplate * @param sqlToyConfig * @param pageModel * @param mql * @param resultClass * @return * @throws Exception */ private PaginationModel findPage(MongoTemplate mongoTemplate, SqlToyConfig sqlToyConfig, PaginationModel pageModel, String mql, Class resultClass) throws Exception { PaginationModel result = new PaginationModel(); result.setPageNo(pageModel.getPageNo()); result.setPageSize(pageModel.getPageSize()); BasicQuery query = new BasicQuery(mql); result.setRecordCount(mongoTemplate.count(query, sqlToyConfig.getNoSqlConfigModel().getCollection())); // 设置分页 if (result.getPageNo() == -1) { query.skip(0).limit(Long.valueOf(result.getRecordCount()).intValue()); } else { query.skip((pageModel.getPageNo() - 1) * pageModel.getPageSize()).limit(pageModel.getPageSize()); } if (sqlToyContext.isDebug()) { if (logger.isDebugEnabled()) { logger.debug("findPageByMongo script=" + query.getQueryObject()); } else { System.out.println("findPageByMongo script=" + query.getQueryObject()); } } List<Document> rs = mongoTemplate.find(query, Document.class, sqlToyConfig.getNoSqlConfigModel().getCollection()); if (rs == null || rs.isEmpty()) { return result; } result.setRows(extractFieldValues(sqlToyConfig, rs.iterator(), resultClass)); return result; }
Example 2
Source File: Mongo.java From sagacity-sqltoy with Apache License 2.0 | 5 votes |
/** * @todo 取top记录 * @param mongoTemplate * @param sqlToyConfig * @param topSize * @param mql * @param resultClass * @return */ private List<?> findTop(MongoTemplate mongoTemplate, SqlToyConfig sqlToyConfig, Float topSize, String mql, Class resultClass) throws Exception { BasicQuery query = new BasicQuery(mql); if (topSize != null) { if (topSize > 1) { query.limit(topSize.intValue()); } else { // 按比例提取 long count = mongoTemplate.count(query, sqlToyConfig.getNoSqlConfigModel().getCollection()); query.limit(Double.valueOf(count * topSize.floatValue()).intValue()); } } if (sqlToyContext.isDebug()) { if (logger.isDebugEnabled()) { logger.debug("findTopByMongo script=" + query.getQueryObject()); } else { System.out.println("findTopByMongo script=" + query.getQueryObject()); } } List<Document> rs = mongoTemplate.find(query, Document.class, sqlToyConfig.getNoSqlConfigModel().getCollection()); if (rs == null || rs.isEmpty()) { return null; } return extractFieldValues(sqlToyConfig, rs.iterator(), resultClass); }