java mongodb order_利用javaAPI操作mongoDB

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 studentCollection = db.getCollection(MONGO_COLLECTION_STUDENT);

static MongoCollection teacherCollection = db.getCollection(MONGO_COLLECTION_TEACHER);

static MongoCollection courseCollection = db.getCollection(MONGO_COLLECTION_COURSE);

static MongoCollection studentCourseCollection = db.getCollection(MONGO_COLLECTION_STUDENT_COURSE);

static MongoCollection 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 aggre = new ArrayList();

aggre.add(group);

MongoCursor 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 aggre = new ArrayList();

aggre.add(group);

aggre.add(sort);

aggre.add(limit);

MongoCursor cursor = studentCourseCollection.aggregate(aggre).iterator();

List 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"));

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值