java MongoDB 测试demo 工具包
/**
* 描述:
* MongoDB 测试demo 工具包
* @author 闲走天涯
* @create 2021/7/22 15:09
*/
public class MongoDBJDBC {
private static String host="localhost";
private static Integer port = 27017;
/**
* 连接 数据库
* @param dbName
* @return
*/
public static MongoDatabase connect(String dbName){
try {
if(strIsNull(dbName)){
return null;
}
// 连接到 MongoDB服务
MongoClient mongoClient = new MongoClient(host, port);
String point = mongoClient.getConnectPoint();
System.out.println("point="+point);
//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase(dbName);
System.out.println("connent to MongoDB database "+dbName+" successfully");
return mongoDatabase;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
/**
* 连接 数据库
* 认证用户名密码
* @param username
* @param password
* @param dbName
* @return
*/
public static MongoDatabase connect(String username,String password,String dbName){
try {
if(strIsNull(dbName) && strIsNull(username) && strIsNull(password)){
return null;
}
//连接到MongoDB服务
ServerAddress serverAddress = new ServerAddress(host,port);
List<ServerAddress> addressList = new ArrayList<>();
addressList.add(serverAddress);
// MongoCredential 三个参数分别为用户名,数据库名,密码
//连接认证信息
MongoCredential credential = MongoCredential.createScramSha1Credential(username,dbName,password.toCharArray());
List<MongoCredential> credentialList = new ArrayList<>();
credentialList.add(credential);
//通过连接认证获取MongoDB连接
MongoClient mongoClient = new MongoClient(addressList,credentialList);
String point = mongoClient.getConnectPoint();
System.out.println("point="+point);
//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase(dbName);
System.out.println("connent to MongoDB database "+dbName+" successfully");
return mongoDatabase;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
public static BasicDBObject orderBy(String order,ByUnit by){
BasicDBObject sort = new BasicDBObject();
sort.put(order,by.getBy());
return sort;
}
/**
* 判断字符串是否为空
* @param str
* @return
*/
public static boolean strIsNull(String str){
return !(str!=null && !"".equals(str));
}
public static void main(String[] args) {
MongoDatabase mongoDatabase = MongoDBJDBC.connect("test","123456","test");
MongoCollection<Document> collection = mongoDatabase.getCollection("runoob");
//集合-》查询
FindIterable<Document> findIterable = null;
//查询所有
findIterable = collection.find();
//使用正则表达式查询
findIterable = collection.find(Filters.regex("name","/菜鸟/"));
//使用条件查询
//匹配小于20
findIterable = collection.find(Filters.lt("age",20));
//匹配小于或等于
findIterable = collection.find(Filters.lte("age",20));
//匹配等于
findIterable = collection.find(Filters.eq("age",15));
//匹配大于20
findIterable = collection.find(Filters.gt("age",10));
//匹配大于或等于
findIterable = collection.find(Filters.gte("age",10));
//匹配数组中指定的值
findIterable = collection.find(Filters.in("age",10));
//不匹配数组中任何指定的任何值=> not in
findIterable = collection.find(Filters.nin("age",10));
//匹配不等于指定值的值
findIterable = collection.find(Filters.ne("age",10));
//条件组合- 与
findIterable = collection.find(Filters.and(Filters.regex("name","/菜鸟/"),Filters.lt("age","20")));
//条件组合- 或
findIterable = collection.find(Filters.or(Filters.regex("name","/菜鸟/"),Filters.lt("age","20")));
//条件组合- 非 (单个子句/条件)
findIterable = collection.find(Filters.not(Filters.regex("name","/菜鸟/")));
//条件组合- 非 (多个子句/条件)
findIterable = collection.find(Filters.or(Filters.regex("name","/菜鸟/"),Filters.lt("age","20")));
//查询是否存在的值 true=存在(默认),false=不存在
findIterable = collection.find(Filters.exists("age"));//查询存在 默认存在
findIterable = collection.find(Filters.exists("age",false));//查询不存在 true=存在 ,false=不存在
//查询匹配类型的值,例如 匹配name为string类型的值
findIterable = collection.find(Filters.type("name",BsonType.STRING));
// 排序 按照字段
findIterable = collection.find().sort(MongoDBJDBC.orderBy("age",ByUnit.DESC));
//限制条数
findIterable = collection.find().limit(2);
//从下标数开始 例如 从下标为2,即第三个开始
findIterable = collection.find().skip(2);
//分页 limit=2 限制每页条数为2 skip=2=2*1 从第2页开始即第三个开始(页码*页数=记录的下标数)
// 相当于 SELECT * FROM table LIMIT 2,2;//查询第3-4行记录
findIterable = collection.find().sort(MongoDBJDBC.orderBy("age",ByUnit.DESC)).limit(2).skip(2);
MongoCursor<Document> mongoCursor = findIterable.iterator();
while (mongoCursor.hasNext()){
Document document = mongoCursor.next();
Object o = document.entrySet();
Object values = document.values();
JSONObject json = JSONObject.parseObject(document.toJson());
System.out.println(document);
}
}
}
/**
* 枚举类型
* 排序asc/desc
*/
enum ByUnit{
ASC(1),
DESC(-1);
private Integer by;
ByUnit(Integer by){
this.by=by;
}
public Integer getBy() {
return by;
}
}