天天看点

php mongo多条件查询,mongodb多条件分页查询

被教作人篇--我不知道的写法

@Autowired

private MongoTemplate mongoTemplate;

@Autowired

private MongoPageHandler mongoPageHandler;

public List handler(QueryDataByConditionsCommand command) throws EasywareException {

Criteria criteria = new Criteria();

Validate.execAssert(command.getInstanceItems()!=null,"请设置默认查询条件");

mongoPageHandler.jointParams(command.getInstanceItems(),criteria);

Pageable pageable = new PageRequest(command.getOffset(), command.getLimit());

List results = mongoTemplate.find(new Query().addCriteria(criteria).with(pageable).with(new Sort(Direction.ASC, "date")),Map.class, command.getEntityName());

return results;

}

MongoPageHandler:

import com.easyware.comman.controller.PagedQueryResult;

import com.easyware.comman.domain.AuditEntity;

import com.google.common.collect.Lists;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Sort;

import org.springframework.data.domain.Sort.Direction;

import org.springframework.data.domain.Sort.Order;

import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.query.Criteria;

import org.springframework.data.mongodb.core.query.Query;

import org.springframework.stereotype.Component;

@Component

public class MongoPageHandler {

public static final int FIRST_PAGE_NUM = 1;

public static final String ID = "_id";

public static final String CREATE_TIME = "createTime";

private final MongoTemplate mongoTemplate;

@Autowired

public MongoPageHandler(MongoTemplate mongoTemplate) {

this.mongoTemplate = mongoTemplate;

}

public PagedQueryResult pageQuery(Map paramsMap, Class entityClass, Integer pageSize, Integer pageNum, String lastId, boolean isNextPage) {

Query query = new Query();

Criteria criteria = new Criteria();

this.jointParams(paramsMap, criteria);

query.addCriteria(criteria);

long total = this.mongoTemplate.count(query, entityClass);

Integer pages = (int)Math.ceil((double)total / (double)pageSize);

if (pageNum <= 0 || pageNum > pages) {

pageNum = 1;

}

if (pageNum != 1) {

if (isNextPage) {

criteria.and("_id").gt(UUID.fromString(lastId));

} else {

criteria.and("_id").lt(UUID.fromString(lastId));

}

}

query.limit(pageSize);

query.addCriteria(criteria);

List entityList = this.mongoTemplate.find(query, entityClass);

PagedQueryResult pagedQueryResult = new PagedQueryResult();

pagedQueryResult.setTotal((int)total);

pagedQueryResult.setList(entityList);

return pagedQueryResult;

}

public PagedQueryResult pageQuery(Map paramsMap, Class entityClass, Integer limit, Integer offset, Sort sort, String collectionName) {

Query query = new Query();

Criteria criteria = new Criteria();

this.jointParams(paramsMap, criteria);

query.addCriteria(criteria);

long total = collectionName == null ? this.mongoTemplate.count(query, entityClass) : this.mongoTemplate.count(query, entityClass, collectionName);

Integer pages = (int)Math.ceil((double)total / (double)limit);

PagedQueryResult pagedQueryResult = new PagedQueryResult();

pagedQueryResult.setTotal((int)total);

if (offset <= 0) {

offset = 1;

} else if (offset > pages) {

return pagedQueryResult;

}

if (null != sort) {

query.with(sort);

} else {

query.with(Sort.by(Lists.newArrayList(new Order[]{new Order(Direction.DESC, "createTime")})));

}

int skip = limit * (offset - 1);

query.skip((long)skip).limit(limit);

List entityList = collectionName == null ? this.mongoTemplate.find(query, entityClass) : this.mongoTemplate.find(query, entityClass, collectionName);

pagedQueryResult.setList(entityList);

return pagedQueryResult;

}

public void jointParams(Map paramsMap, Criteria criteria) {

Map map = new HashMap();

Iterator var4 = paramsMap.keySet().iterator();

while(var4.hasNext()) {

String col = (String)var4.next();

String str;

Object obj;

MongoPageHandler.ScopeParams scopeParams;

if (col.contains("From")) {

str = col.replace("From", "");

obj = paramsMap.get(col);

scopeParams = (MongoPageHandler.ScopeParams)map.get(str);

if (scopeParams == null) {

scopeParams = new MongoPageHandler.ScopeParams();

scopeParams.name = str;

map.put(str, scopeParams);

}

scopeParams.gteValue = obj;

} else if (col.contains("To")) {

str = col.replace("To", "");

obj = paramsMap.get(col);

scopeParams = (MongoPageHandler.ScopeParams)map.get(str);

if (scopeParams == null) {

scopeParams = new MongoPageHandler.ScopeParams();

scopeParams.name = str;

map.put(str, scopeParams);

}

scopeParams.lteValue = obj;

} else if (paramsMap.get(col) instanceof String) {

str = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$";

if (((String)paramsMap.get(col)).matches("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")) {

criteria.and(col).is(UUID.fromString(paramsMap.get(col).toString()));

System.out.println(UUID.fromString(paramsMap.get(col).toString()));

} else {

criteria.and(col).regex(".*?" + paramsMap.get(col) + ".*", "i");

}

} else {

criteria.and(col).is(paramsMap.get(col));

}

}

var4 = map.values().iterator();

while(true) {

while(var4.hasNext()) {

MongoPageHandler.ScopeParams sp = (MongoPageHandler.ScopeParams)var4.next();

if (sp.gteValue != null && sp.lteValue != null) {

criteria.and(sp.name).gte(sp.gteValue).lte(sp.lteValue);

} else if (sp.gteValue != null) {

criteria.and(sp.name).gte(sp.gteValue);

} else {

criteria.and(sp.name).lte(sp.lteValue);

}

}

return;

}

}

public class ScopeParams {

public String name;

public Object gteValue;

public Object lteValue;

public ScopeParams() {

}

}

}