由于最近项目要使用mongodb来处理一些日志,提前学习了一下mongodb的一些基本用法,大概写了一些常用的。
开发环境为:WIN7-64,JDK7-64,MAVEN3.3.9-64,IDEA2017-64.
程序基本结构为:

下面贴出核心代码示例:
pom.xml
4.0.0
TestWebProjectMaven
TestWebProjectMaven
war
1.0-SNAPSHOT
TestWebProjectMaven Maven Webapp
nexus-repos
Team Nexus Repository
http://192.168.200.205:8081/nexus/content/groups/public/
true
true
nexus-repos
Team Nexus Repository
http://192.168.200.205:8081/nexus/content/groups/public/
true
true
junit
junit
3.8.1
test
org.springframework
spring-webmvc
4.1.6.RELEASE
org.springframework
spring-context
4.1.6.RELEASE
org.mongodb
mongo-java-driver
3.2.1
org.jetbrains
annotations-java5
RELEASE
commons-configuration
commons-configuration
1.10
TestWebProjectMaven
src/main/java
**/*.properties
src/main/resources
mongodb.properties
MONGODB_IP=192.168.200.234
MONGODB_PORT=10143
MONGODB_DATABASE_NAME=runoob
MONGODB_COLLECTION_NAME=test
MongodbUtil
package org.mbox.util;
import com.mongodb.MongoClient;
import com.mongodb.client.*;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.result.DeleteResult;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import org.mbox.model.PageVO;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
/**
* Created by pc on 2017/6/5.
*/
public class MongodbUtil {
private static MongoClient MONGODB_CLIENT = null;
private static String MONGODB_IP = null;
private static Integer MONGODB_PORT = null;
private static String MONGODB_DATABASE_NAME = null;
private static String MONGODB_COLLECTION_NAME = null;
static{
CompositeConfiguration compositeConfiguration = new CompositeConfiguration();
try {
compositeConfiguration.addConfiguration(new PropertiesConfiguration("mongodb.properties"));
} catch (ConfigurationException e) {
e.printStackTrace();
}
MONGODB_IP = compositeConfiguration.getString("MONGODB_IP");
MONGODB_PORT = compositeConfiguration.getInt("MONGODB_PORT");
MONGODB_DATABASE_NAME = compositeConfiguration.getString("MONGODB_DATABASE_NAME");
MONGODB_COLLECTION_NAME = compositeConfiguration.getString("MONGODB_COLLECTION_NAME");
MONGODB_CLIENT = new MongoClient(MONGODB_IP,MONGODB_PORT);
}
private MongodbUtil() {
}
/**
* 初始化mongodb数据源
* @return
*/
public static MongoDatabase getMongodbDatabase(){
return MONGODB_CLIENT.getDatabase(MONGODB_DATABASE_NAME);
}
/**
* 关闭MongoClient连接
*/
public static void closeMongodbClient(){
if(null != MONGODB_CLIENT){
MONGODB_CLIENT.close();
MONGODB_CLIENT = null;
}
}
/**
* 获取mongodb的表对象
* @return
*/
public static MongoCollection getMongoCollection(){
return getMongodbDatabase().getCollection(MONGODB_COLLECTION_NAME);
}
/**
* 通过map插入一条数据到表中
* @param map
*/
public static void insertOneCollectionByMap(Map map){
getMongoCollection().insertOne(handleMap(map));
}
/**
* 通过集合map一次性插入多条数据到表中
* @param listMap
*/
public static void insertManyCollectionByMap(List