码迷,mamicode.com
首页 > 数据库 > 详细

MongoDB多条件分页查询,新增,删除操作

时间:2015-05-26 14:25:26      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:mongodb查询语句   mongodb java   日期范围   mongodb分页   删除   

概述

日志信息记录到MongoDB中,然后多条件查询

程序界面

技术分享

MongoDBTools.java

package com.admin.utils;

import java.lang.reflect.Field;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.admin.entity.LogEntity;
import com.admin.entity.PageModel;
import com.admin.entity.SearchEntity;
import com.google.gson.Gson;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

/**
 * @Description:测试MongoDB的基本操作
 * @author limh
 * @date 2015年5月14日
 * @time 下午3:24:58
 */
public class MongoDBTools {

	private static final Logger logger = Logger.getLogger(MongoDBTools.class);
	private static Mongo mongo;
	private static final String host = "192.168.15.115";
	private static final int port = 10001;
	private static final String dbName = "shuju";
	private static DB db;
	private static final String tableName = "logTable";

	static {
		try {
			mongo = new Mongo(host, port);
			db = mongo.getDB(dbName); // 取得指定的库
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Description 测试
	 * @version 1.0
	 * @author limh
	 * @throws MongoException
	 * @throws UnknownHostException
	 * @date 2015年5月14日
	 * @time 下午3:25:00
	 */
	public static void main(String[] args) {
		try {
			// 日志记录实体类
			//LogEntity logEntity = new LogEntity("1002", "crawler001", "thread001", "100.100.100.100", "paipaidai", "2015-05-23 23:18:51", "测试数据02");
			// 录入数据
			//add(logEntity);
			
			// 删除数据
			//DBObject delLog = new BasicDBObject();
			// 删除 crawlerId 字段 为 “crawler001”的数据    相当于:delete from table t where t.crawlerId = 'crawler001';
			// delLog.put("crawlerId", "crawler001");
			// dgLogTable.remove(delLog);
			
			// 查询全部数据
			//queryAll(tableName);

			// 条件查询
			// 相当于sql中: logTime = '2015-05-20' and platId like '%paipai%' or crawlerId like '%paipai%'
			SearchEntity searchEntity = new SearchEntity("all", "paipai", "2015-05-20", "2015-05-20");
			// 相当于sql中: logTime >= '2015-05-20' and logTime < '2015-05-21' and  platId like '%aip%'
			searchEntity = new SearchEntity("platId", "aip", "2015-05-20", "2015-05-21");
			PageModel pageModel = new PageModel(20,1);
			String platId = "";
			
			List<LogEntity> list = largePageList(tableName, searchEntity, pageModel, platId);
			for (LogEntity logE : list) {
				System.out.println(logE);
			}
		} catch (MongoException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @Description 查询指定表的全部数据
	 * @version 1.0
	 * @author limh
	 * @date 2015年5月14日
	 * @time 下午5:41:53
	 */
	public static List<LogEntity> queryAll(String tableName) {
		logger.info("查询" + tableName + "表的所有数据:"); // db游标
		List<LogEntity> list = new ArrayList<LogEntity>();
		// 指定表名
		DBCollection dgLogTable = db.getCollection(tableName);
		DBCursor cur = dgLogTable.find();
		System.out.println(tableName+"一共有:"+cur.count()+"条数据");
		while (cur.hasNext()) {
			DBObject dbObject = cur.next();
			Gson gson = new Gson();
			// 把Mongodb的DBObject对象,转成Java对象
			LogEntity log = gson.fromJson(dbObject.toString(), LogEntity.class);
			list.add(log);
			//logger.info(log);
		}
		return list;
	}

	/**
	 * 
	 * @Description 获取指定集合的数据总数
	 * @version 1.0
	 * @author limh
	 * @date 2015年5月18日
	 * @time 下午5:02:13
	 */
	public static long getMongoCount(String tbName) {
		// 指定表名
		DBCollection dbLogTable = db.getCollection(tbName);
		long result = dbLogTable.count();
		return result;
	}

	/**
	 * 
	 * @Description 向MongoDB添加数据
	 * @version 1.0
	 * @author limh
	 * @date 2015年5月14日
	 * @time 下午5:44:29
	 */
	@SuppressWarnings("unused")
	private static void add(LogEntity logEntity) {
		DBCollection dgLogTable = db.getCollection(tableName);
		// 录入操作
		DBObject log = fillEntity(logEntity);
		logger.info(log);
		int result = dgLogTable.save(log).getN();
		logger.info("录入结果:" + result);
	}

	/**
	 * 
	 * @Description 通过反射,给定的实体类的 成员变量名为key、值为 value,填充 MongoDB 的 DBObject 对象
	 * @version 1.0
	 * @author limh
	 * @date 2015年5月14日
	 * @time 下午5:58:28
	 */
	private static DBObject fillEntity(LogEntity logEntity) {
		DBObject log = new BasicDBObject();
		Class<?> cls = logEntity.getClass();
		try {
			// 获取到 类声明的成员变量
			Field[] fields = cls.getDeclaredFields();
			for (Field field : fields) {
				// 成员变量的名字
				String fieldNameStr = field.getName();
				field.setAccessible(true);
				// 成员变量的值
				Object temp = field.get(logEntity);
				log.put(fieldNameStr, temp);
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return log;
	}

	/**
	 * 大数据量数据分页优化
	 * 
	 * @param 表名称
	 *            (集合名称)
	 * @param page
	 * @param pageSize
	 * @param lastId
	 *            上一页的最大id
	 * @return
	 */
	public static List<LogEntity> largePageList(String tableName, SearchEntity searchEntity, PageModel pageModel, String platId) {
		// 指定表名
		DBCollection dgLogTable = db.getCollection(tableName);
		DBCursor dbCursor = null;
		// 排序
		BasicDBObject sortDBObject = new BasicDBObject("logTime", -1);
		// 是否需要按条件筛选数据
		boolean ifConditionQuery = StringUtils.isBlank(searchEntity.getKeyWord());

		// 搜索关键字
		String keyWord = searchEntity.getKeyWord();
		// 搜索依据哪个字段
		String selVal = searchEntity.getSelVal();
		// 开始日期
		String startDate = searchEntity.getStartDate();
		// 结束日期
		String endDate = searchEntity.getEndDate();
		// 模糊匹配的正则表达式
		String reg = ".*" + keyWord + ".*";

		// 查询条件汇总
		BasicDBObject totalCon = new BasicDBObject();
		BasicDBList dateDbList = new BasicDBList();

		// 如果平台ID参数不是空,只查询指定平台的ID
		if (StringUtils.isNotBlank(platId)) {
			// 相当于sql中添加条件限制: and platId = platId
			totalCon.put("platId", platId);
		}

		// 默认设置:当开始日期和结束日期都不为空的时候,再进行日期限制查询
		if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
			// >=startDate
			BasicDBObject startCondition = new BasicDBObject("$gte", startDate);
			//注意: mongoDB中,2015.05.20——2015.05.20,查不到数据,需要查询:2015.05.20——2015.05.21的数据,查的是20号的(<21,不包含21号数据)
			// <endDate
			BasicDBObject endCondition = new BasicDBObject("$lt", Tools.addOneDay(endDate));

			dateDbList.add(new BasicDBObject("logTime", startCondition));
			dateDbList.add(new BasicDBObject("logTime", endCondition));
			// 汇总条件中,添加日期条件		相当于sql中的: logTime >= startDate and logTime<endDate
			totalCon.put("$and", dateDbList);
		}

		if (ifConditionQuery) {// 不加关键字条件筛选的情况,全查
			dbCursor = dgLogTable.find(totalCon).sort(sortDBObject);
		} else {
			if ("all".equals(searchEntity.getSelVal())) {// 需要使用or操作,不限制字段进行查询
				// 多个字段之间的OR操作
				BasicDBList fieldsDBList = new BasicDBList();

				Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);

				BasicDBObject conditionPlatId = new BasicDBObject("platId", pattern);
				BasicDBObject conditionCrawlerId = new BasicDBObject("crawlerId", pattern);
				fieldsDBList.add(conditionPlatId);
				fieldsDBList.add(conditionCrawlerId);
				// 相当于sql中: platId like '%keyWord%' or crawlerId like '%keyWord%'
				totalCon.put("$or", fieldsDBList);

				dbCursor = dgLogTable.find(totalCon).sort(sortDBObject);
			} else {// 明确指定字段的查询
				Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
				// 汇总条件中,添加上具体字段的筛选条件
				totalCon.put(selVal, pattern);
				dbCursor = dgLogTable.find(totalCon).sort(sortDBObject);
			}
		}

		// 设置数据总数
		long total = dbCursor.count();
		pageModel.setTotal(total);
		// 设置分页总数
		pageModel.setPageCounter(PageModel.calcPageCounter(total, pageModel.getPageSize()));

		int pageNo = pageModel.getPageNo();
		int pageSize = pageModel.getPageSize();
		// 如果当前页数大于总页数
		if (pageNo > pageModel.getPageCounter()) {
			// 跳转到首页
			pageNo = 1;
		}
		/** 分页 */
		int skipNum = (pageNo - 1) * pageSize;
		// 查询第一页数据
		if (pageNo == 1) {
			dbCursor = dbCursor.limit(pageSize);
		} else {// 非第一页数据
			dbCursor = dbCursor.skip(skipNum).limit(pageSize);
		}
		
		// 查询结果
		List<LogEntity> logList = new ArrayList<LogEntity>();
		while (dbCursor.hasNext()) {
			LogEntity logEntity = new LogEntity();
			DBObject dbObject = dbCursor.next();
			Gson gson = new Gson();
			// 把Mongodb的DBObject对象,转成Java对象
			logEntity = gson.fromJson(dbObject.toString(), LogEntity.class);
			logList.add(logEntity);
		}
		return logList;
	}
}

实体类 LogEntity.java

package com.admin.entity;

/**
 * @Description:日志实体类
 * @author limh
 * @date 2015年5月14日
 * @time 下午5:06:05
 */
public class LogEntity {

	private String id;// 唯一标识
	private String crawlerId;// 爬虫id
	private String threadId;// 线程id
	private String ip;
	private String platId;// 平台id
	private String logTime;// 记录的时间
	private String detail;// 详情

	/**
	 * @Description:无参构造器
	 * @author limh
	 * @date 2015年5月14日
	 * @time 下午5:15:39
	 */
	public LogEntity() {
		super();
	}

	/**
	 * @Description:
	 * @param id
	 * @param crawlerId
	 * @param threadId
	 * @param ip
	 * @param platId
	 * @param logTime
	 * @param detail
	 * @author limh
	 * @date 2015年5月14日
	 * @time 下午5:15:30
	 */
	public LogEntity(String id, String crawlerId, String threadId, String ip,
			String platId, String logTime, String detail) {
		super();
		this.id = id;
		this.crawlerId = crawlerId;
		this.threadId = threadId;
		this.ip = ip;
		this.platId = platId;
		this.logTime = logTime;
		this.detail = detail;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getCrawlerId() {
		return crawlerId;
	}

	public void setCrawlerId(String crawlerId) {
		this.crawlerId = crawlerId;
	}

	public String getThreadId() {
		return threadId;
	}

	public void setThreadId(String threadId) {
		this.threadId = threadId;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getPlatId() {
		return platId;
	}

	public void setPlatId(String platId) {
		this.platId = platId;
	}

	public String getLogTime() {
		return logTime;
	}

	public void setLogTime(String logTime) {
		this.logTime = logTime;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("LogEntity [id=");
		builder.append(id);
		builder.append(", crawlerId=");
		builder.append(crawlerId);
		builder.append(", threadId=");
		builder.append(threadId);
		builder.append(", ip=");
		builder.append(ip);
		builder.append(", platId=");
		builder.append(platId);
		builder.append(", logTime=");
		builder.append(logTime);
		builder.append(", detail=");
		builder.append(detail);
		builder.append("]");
		return builder.toString();
	}
}

查询条件实体类 SearchEntity.java

package com.admin.entity;

/**
 * @Description:用于封装搜索因子数据
 * @author limh
 * @date 2015年5月19日
 * @time 下午2:48:27
 */
public class SearchEntity {

	/** 下拉框筛选条目 */
	private String selVal;
	/** 搜索关键字 */
	private String keyWord;
	/** 开始日期 */
	private String startDate;
	/** 结束日期 */
	private String endDate;
	
	
	/**
	 * @Description:无参数构造器
	 * @author limh
	 * @date 2015年5月19日
	 * @time 下午4:39:46
	 */
	public SearchEntity() {
		super();
	}

	/**
	 * @Description:
	 * @param selVal
	 * @param keyWord
	 * @param startDate
	 * @param endDate
	 * @author limh
	 * @date 2015年5月19日
	 * @time 下午4:39:31
	 */
	public SearchEntity(String selVal, String keyWord, String startDate,
			String endDate) {
		super();
		this.selVal = selVal;
		this.keyWord = keyWord;
		this.startDate = startDate;
		this.endDate = endDate;
	}
	
	public String getSelVal() {
		return selVal;
	}
	public void setSelVal(String selVal) {
		this.selVal = selVal;
	}
	public String getKeyWord() {
		return keyWord;
	}
	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}
	public String getStartDate() {
		return startDate;
	}
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("SearchEntity [selVal=");
		builder.append(selVal);
		builder.append(", keyWord=");
		builder.append(keyWord);
		builder.append(", startDate=");
		builder.append(startDate);
		builder.append(", endDate=");
		builder.append(endDate);
		builder.append(", hashCode()=");
		builder.append(hashCode());
		builder.append("]");
		return builder.toString();
	}
}

工具类 StringUtils.java

注:commons-lang3-3.3.2.jar中的org.apache.commons.lang3.StringUtils类

package com.admin.utils;

/**
 * @Description:工具类
 * @author limh
 * @date 2015年5月26日
 * @time 上午11:34:24
 */
public class StringUtils {

	/**
	 * @Description TODO
	 * @version 1.0
	 * @author limh
	 * @date 2015年5月26日
	 * @time 上午11:34:50
	 */
	public static boolean isBlank(final CharSequence cs) {
		int strLen;
		if (cs == null || (strLen = cs.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if (Character.isWhitespace(cs.charAt(i)) == false) {
				return false;
			}
		}
		return true;
	}

	/**
	 * @Description TODO
	 * @version 1.0
	 * @author limh
	 * @date 2015年5月26日
	 * @time 上午11:35:58
	 */
	public static boolean isNotBlank(final CharSequence cs) {
		return !StringUtils.isBlank(cs);
	}

}

转载请注明出处:http://blog.csdn.net/limenghua9112/article/details/46006937

日期工具类 Tools.java

package com.admin.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Tools {

	public static final String DESC = "desc";
	public static final String ASC = "asc";
	public static final String dateFormat = "yyyy-MM-dd";
	public static final SimpleDateFormat format = new SimpleDateFormat(dateFormat);
	private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

	
	 /**
     * Stringate
     * @param strDate
     * @return
     * @throws Exception
     */
	public static Date toDate(String strDate){
		Date date = null;
		try {
			date = df.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}


	/**
	 * 
	 * @Description 传递过来的日期加一天返回,String 类型
	 * @version	1.0
	 * @author limh
	 * @date 2015年5月20日
	 * @time 下午10:27:21
	 */
	public static String addOneDay(String dateStr) {
		String resultDate = "";
		try {
			Date date = (new SimpleDateFormat("yyyy-MM-dd")).parse(dateStr);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.DATE, 1);
			resultDate = (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
		} catch (ParseException e) {
			
		}
		return resultDate;
	}
}

pom.xml

<!-- log4j -->
<dependency>
	<groupId>log4j</groupId>
	<artifactId>log4j</artifactId>
	<version>1.2.17</version>
</dependency>
<!-- mongodb -->
<dependency>
	<groupId>org.mongodb</groupId>
	<artifactId>mongo-java-driver</artifactId>
	<version>2.5.3</version>
</dependency>
<!-- gson -->
<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>2.3.1</version>
</dependency>


查询结果

1、

// 相当于sql中: logTime = '2015-05-20' and platId like '%paipai%' or crawlerId like '%paipai%'
SearchEntity searchEntity = new SearchEntity("all", "paipai", "2015-05-20", "2015-05-20");


LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-20 13:18:51, detail=测试数据02]
LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-20 00:18:51, detail=测试数据02]
LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-20 00:18:51, detail=测试数据02]
LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-20 00:18:51, detail=测试数据02]
LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-20, detail=测试数据02]
LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-20, detail=测试数据02]
LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-20, detail=测试数据02]

2、

// 相当于sql中: logTime >= '2015-05-20' and logTime < '2015-05-21' and  platId like '%aip%'
searchEntity = new SearchEntity("platId", "aip", "2015-05-20", "2015-05-21");

LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-21, detail=测试数据02]
LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-21, detail=测试数据02]
LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-20 13:18:51, detail=测试数据02]
LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-20 00:18:51, detail=测试数据02]
LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-20 00:18:51, detail=测试数据02]
LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-20 00:18:51, detail=测试数据02]
LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-20, detail=测试数据02]
LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-20, detail=测试数据02]
LogEntity [id=1002, crawlerId=crawler001, threadId=thread001, ip=100.100.100.100, platId=paipaidai, logTime=2015-05-20, detail=测试数据02]

总结

MongoDB和普通Sql查询思维不太一样,折腾了好几天的成果,现在写在这里,希望能帮助其他正好用到MongoDB查询的朋友。





MongoDB多条件分页查询,新增,删除操作

标签:mongodb查询语句   mongodb java   日期范围   mongodb分页   删除   

原文地址:http://blog.csdn.net/limenghua9112/article/details/46006937

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!