用java做MongoDB的简单查询
环境配置
首先谈一下导入jar的问题,网上基本可以查到导入3个包(bson.jar、mongodb-driver.jar、mongodb-driver-core.jar),但是个人推荐用三合一的包。直接上图(注意千万别弄上面那个,坑的很,要是导入上面那个会一直提示你MongoDBClient可以在两个地方引用到,还不告你怎么解决)
代码
package nosql;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.conversions.Bson;
import java.io.FileFilter;
import org.bson.Document;
public class MongoDB_Test {
public static void main(String[] args) {
MongoDB_Test mgt=new MongoDB_Test();
mgt.find_age("user", "student");//找出年龄小于20岁的学生(1)
//mgt.find_age_dname("user", "student");//找出20岁以下的软件学院的学生(2)
//mgt.find_name_age("user", "student");//找所有学生的年龄和姓名(3)
//mgt.find_allstudents("user", "student");//找所有学生(4)
//mgt.find_name_sex("user", "student");//找出20岁以下学生的姓名和性别(5)
//mgt.find_course("user", "course");//检索所有课程情况(6)
//mgt.find_course_name("user", "course");//检索先行课号为“300001”的课程名(7)
mgt.find_teacher_age("user", "teacher");//找出年龄大于50的老师(8)
//mgt.find_sex("user", "teacher");//找出所有男老师(9)
//mgt.find_dname("user", "teacher");//找出所以CS学院老师(10)
}
//找出年龄小于20的学生
public void find_age(String database_name,String collection_name) {
MongoClient client=new MongoClient("127.0.0.1",27017);
MongoDatabase dbs=client.getDatabase(database_name);
MongoCollection<Document> collection=dbs.getCollection(collection_name);
BasicDBObject query=new BasicDBObject();
query.put("AGE", new BasicDBObject("$lt","20"));
MongoCursor<Document> cusor=collection.find(query).iterator();
while(cusor.hasNext()) {
Document document=cusor.next();
System.out.println("sid:"+document.getString("\ufeffSID")+"\tname:"+document.getString("NAME")+"\tsex:"+
document.getString("SEX")+"\tage:"+document.getString("AGE")+"\tbirrhday:"+
document.getString("BIRTHDAY")+"\tdname"+document.getString("DNAME")+
"\tclass:"+document.getString("CLASS"));
}
client.close();
}
//找出年龄小于20的软件学院学生
public void find_age_dname(String database_name,String collection_name) {
MongoClient client=new MongoClient("127.0.0.1",27017);
MongoDatabase dbs=client.getDatabase(database_name);
MongoCollection<Document> collection=dbs.getCollection(collection_name);
BasicDBObject query=new BasicDBObject();
query.put("AGE", new BasicDBObject("$lt","20"));
query.put("DNAME", "软件学院");
MongoCursor<Document> cusor=collection.find(query).iterator();
while(cusor.hasNext()) {
Document document=cusor.next();
System.out.println("sid:"+document.getString("\ufeffSID")+"\tname:"+document.getString("NAME")+"\tsex:"+
document.getString("SEX")+"\tage:"+document.getString("AGE")+"\tbirrhday:"+document.getString("BIRTHDAY")+"\tdname"+document.getString("DNAME")+
"\tclass:"+document.getString("CLASS"));
}
client.close();
}
//找出所有学生
public void find_allstudents(String database_name,String collection_name) {
MongoClient client=new MongoClient("127.0.0.1",27017);
MongoDatabase dbs=client.getDatabase(database_name);
MongoCollection<Document> collection=dbs.getCollection(collection_name);
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> cursor = findIterable.iterator();
while (cursor.hasNext()) {
Document document=cursor.next();
System.out.println("sid:"+document.getString("\ufeffSID")+"\tname:"+document.getString("NAME")+"\tsex:"+
document.getString("SEX")+"\tage:"+document.getString("AGE")+"\tbirrhday:"+document.getString("BIRTHDAY")+"\tdname"+document.getString("DNAME")+
"\tclass:"+document.getString("CLASS"));
}
client.close();
}
//找所有学生的年龄和姓名
public void find_name_age(String database_name,String collection_name) {
MongoClient client=new MongoClient("127.0.0.1",27017);
MongoDatabase dbs=client.getDatabase(database_name);
MongoCollection<Document> collection=dbs.getCollection(collection_name);
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> cursor = findIterable.iterator();
while (cursor.hasNext()) {
Document documen=cursor.next();
System.out.println("name:"+documen.getString("NAME")+"\tage:"+documen.getString("AGE"));
}
client.close();
}
//找出20岁以下学生的姓名和性别
public void find_name_sex(String database_name,String collection_name) {
MongoClient client=new MongoClient("127.0.0.1",27017);
MongoDatabase dbs=client.getDatabase(database_name);
MongoCollection<Document> collection=dbs.getCollection(collection_name);
BasicDBObject query=new BasicDBObject();
query.put("AGE", new BasicDBObject("$lt","20"));
MongoCursor<Document> cusor=collection.find(query).iterator();
while(cusor.hasNext()) {
Document document=cusor.next();
System.out.println("name:"+document.getString("NAME")+"\tsex:"+document.getString("SEX"));
}
client.close();
}
//检索所有课程情况
public void find_course(String database_name,String collection_name) {
MongoClient client=new MongoClient("127.0.0.1",27017);
MongoDatabase dbs=client.getDatabase(database_name);
MongoCollection<Document> collection=dbs.getCollection(collection_name);
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> cursor = findIterable.iterator();
while (cursor.hasNext()) {
Document documen=cursor.next();
System.out.println("cid:"+documen.getString("\ufeffCID")+"\tname:"+documen.getString("NAME")+"\tFCID:"+documen.getString("FCID")+
"\tcredit:"+documen.getString("CREDIT"));
}
client.close();
}
//检索先行课号为“300001”的课程名
public void find_course_name(String database_name,String collection_name) {
MongoClient client=new MongoClient("127.0.0.1",27017);
MongoDatabase dbs=client.getDatabase(database_name);
MongoCollection<Document> collection=dbs.getCollection(collection_name);
Bson filter=Filters.eq("FCID","300001");
FindIterable<Document> findIterable=collection.find(filter);
MongoCursor<Document> cursor=findIterable.iterator();
while (cursor.hasNext()) {
Document documen=cursor.next();
System.out.println("name:"+documen.getString("NAME"));
}
client.close();
}
//找出年龄大于50的老师
public void find_teacher_age(String database_name,String collection_name) {
MongoClient client=new MongoClient("127.0.0.1",27017);
MongoDatabase dbs=client.getDatabase(database_name);
MongoCollection<Document> collection=dbs.getCollection(collection_name);
BasicDBObject query=new BasicDBObject();
query.put("AGE", new BasicDBObject("$gt","50"));
MongoCursor<Document> cursor=collection.find(query).iterator();
while (cursor.hasNext()) {
Document document=cursor.next();
System.out.println("TID:"+document.getString("\ufeffTID")+"\tname:"+
document.getString("NAME")+"\tsex:"+document.getString("SEX")+"\tage:"
+document.getString("AGE")+"\tdname"+document.getString("DNAME"));
}
client.close();
}
//找出所有男老师
public void find_sex(String database_name,String collection_name) {
MongoClient client=new MongoClient("127.0.0.1",27017);
MongoDatabase dbs=client.getDatabase(database_name);
MongoCollection<Document> collection=dbs.getCollection(collection_name);
Bson filter=Filters.eq("SEX","男");
//文件过滤器,适合一个筛选条件的 第一个参数是筛选列名 第二个参数是筛选条件的值
FindIterable<Document> findIterable=collection.find(filter);
MongoCursor<Document> cusor=findIterable.iterator();
while(cusor.hasNext()) {
Document document=cusor.next();
System.out.println("TID:"+document.getString("\ufeffTID")+"\tname:"+document.getString("NAME")+"\tsex:"+document.getString("SEX")+"\tage:"
+document.getString("AGE")+"\tdname"+document.getString("DNAME"));
}
client.close();
}
//找出CS学院的老师
public void find_dname(String database_name,String collection_name) {
MongoClient client=new MongoClient("127.0.0.1",27017);
MongoDatabase dbs=client.getDatabase(database_name);
MongoCollection<Document> collection=dbs.getCollection(collection_name);
Bson filter=Filters.eq("DNAME","计算机科学与技术学院");
FindIterable<Document> findIterable=collection.find(filter);
MongoCursor<Document> cusor=findIterable.iterator();
while(cusor.hasNext()) {
Document document=cusor.next();
System.out.println("TID:"+document.getString("\ufeffTID")+"\tname:"+document.getString("NAME")+"\tsex:"+document.getString("SEX")+"\tage:"
+document.getString("AGE")+"\tdname"+document.getString("DNAME"));
}
client.close();
}
}
###有点注意的是关于在获得collection中第一列的值的时候我的key传的是\ufeff+[列名],原因是我在将excel文件转化为csv时选择了utf-8的,然后到时文件开头会有一个这样的字符串,如果小伙伴们文件开头没有这样的字符串可以直接根据列名获取。(刚刚知道好像是我编译器的编码选择问题)
###多条件查询例子
第一次put进去的是年龄小于20
第二次是dname=“软件学院”
然后放到find()去执行。