1.连接数据库,获得集合
private static final String MONGO_HOST = "localhost";
private static final Integer MONGO_PORT = 27017;
private static final String MONGO_DB_NAME = "user201600301248";
private static final String MONGO_COLLECTION_STUDENT = "student";
private static final String MONGO_COLLECTION_TEACHER = "teacher";
private static final String MONGO_COLLECTION_COURSE = "course";
private static final String MONGO_COLLECTION_STUDENT_COURSE = "student_course";
private static final String MONGO_COLLECTION_TEACHER_COURSE = "teacher_course";
private static final String path = "excel/student_course.xlsx";
static MongoClient mongoClient = new MongoClient(MONGO_HOST, MONGO_PORT);
//获取指定数据库
static MongoDatabase db = mongoClient.getDatabase(MONGO_DB_NAME);
//获取指定集合
static MongoCollection<Document> studentCollection = db.getCollection(MONGO_COLLECTION_STUDENT);
static MongoCollection<Document> teacherCollection = db.getCollection(MONGO_COLLECTION_TEACHER);
static MongoCollection<Document> courseCollection = db.getCollection(MONGO_COLLECTION_COURSE);
static MongoCollection<Document> studentCourseCollection = db.getCollection(MONGO_COLLECTION_STUDENT_COURSE);
static MongoCollection<Document> teacherCourseCollection = db.getCollection(MONGO_COLLECTION_TEACHER_COURSE);
2.增加数据
相当于sql中的insert into student(SID,NAME,SEX,AGE,BIRTHDAY,DNAME,CLASS) values("201600301248","胡立国","男",20,"1998/11/16","软件学院","2016")
public static void insertStudent(){
Document document = new Document();
document.put("SID", "201600301248");
document.put("NAME", "胡立国");
document.put("SEX", "男");
document.put("AGE", 20);
document.put("BIRTHDAY","1998/11/16");
document.put("DNAME","软件学院");
document.put("CLASS","2016");
studentCollection.insertOne(document);
}
public void insertStudentCourse(){
Document document = new Document();
document.put("name", "Cheung");
document.put("age", 24);
document.put("address", "Beijing");
document.put("date", new Date());
studentCourseCollection.insertOne(document);
}
3.更新数据
public static void updateStudent(){
BasicDBObject filter = new BasicDBObject();
filter.put("SID","201600301248");
Document document = new Document();
document.put("AGE",50);
studentCollection.updateOne(filter,new Document("$set",document));
}
相当于sql中的update teacher set AGE = 50 where AGE >= 50
public static void updateTeacher(){
teacherCollection.updateMany(
Filters.gte("AGE",50),
new Document("$set",new Document("AGE",50)));
}
4.查询数据
相当于sql中的select * from student where AGE < 20 and DNAME = "软件学院"
public static void searchByAge(){
//sex得大写
String [][]data = new String[3000][7];
BasicDBObject filter = new BasicDBObject();
filter.append("AGE",new BasicDBObject("$lt",20));
filter.append("DNAME","软件学院");
MongoCursor cursor = studentCollection.find(filter).iterator();
int i=0;
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
}
5.聚合查询(对group、limit、order的使用)
private static void searchMaxScoreStudents(){
Document beGroup = new Document();
beGroup.put("_id","$SID");
beGroup.put("max_score",new Document("$max","$SCORE"));
Document group = new Document("$group",beGroup);
List<Document> aggre = new ArrayList();
aggre.add(group);
MongoCursor<Document> cursor = studentCourseCollection.aggregate(aggre).iterator();
while(cursor.hasNext()){
Document document = cursor.next();
String sid = (String) document.get("_id");
int maxScore = (int) document.get("max_score");
MongoCursor cursor1 = studentCollection.find(new Document("SID",sid)).iterator();
if(cursor1.hasNext()) {
String name = (String) ((Document) cursor1.next()).get("NAME");
System.out.println("学号为:" + sid + ",姓名为:" + name + ",最高成绩为:" + maxScore);
}
}
}
相当于sql中的select SID, avg(SCORE) as avg_score from student_course group by SID order by avg_score limit 10
private static void searchTopTenStudents(){
Document beGroup = new Document();
beGroup.put("_id","$SID");
beGroup.put("avg_score",new Document("$avg","$SCORE"));
Document group = new Document("$group",beGroup);
Document beSort = new Document();
beSort.put("avg_score",-1);
Document sort = new Document("$sort",beSort);
Document limit = new Document("$limit",10);
List<Document> aggre = new ArrayList();
aggre.add(group);
aggre.add(sort);
aggre.add(limit);
MongoCursor<Document> cursor = studentCourseCollection.aggregate(aggre).iterator();
List<String> topSid = new ArrayList<>();
while(cursor.hasNext()){
Document document = cursor.next();
String sid = (String) document.get("_id");
topSid.add(sid);
}
for(String sid:topSid){
MongoCursor cursor1 = studentCollection.find(new Document("SID",sid)).iterator();
if(cursor1.hasNext()){
Document nameDoc = (Document) cursor1.next();
System.out.println("学号为:"+nameDoc.get("SID")+",姓名为:"+nameDoc.get("NAME"));
}
}
}