在 Java 程序中如果要使用 MongoDB,你需要确保已经安装了 Java 环境及 MongoDB JDBC 驱动。
首先你必须下载mongo jar包,下载地址:MongoDB Java Drivers, 请确保下载最新版本。
如使用idea的,可通过Maven项目导入mongo jar包,在pom.xml文件中复制以下代码:
org.mongodb mongo-java-driver3.2.2
国内 mongodb-driver jar 下载地址:http://central.maven.org/maven2/org/mongodb/mongo-java-driver/
准备工作//连接到 mongo 服务,默认连接到localhost服务器,端口为27017
MongoClient mongoClient = new MongoClient();
//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
//获取学生集合
MongoCollection students = mongoDatabase.getCollection("students");
System.out.println("集合选择成功");
本文以下列数据为例
{ "_id" : ObjectId("628b721b642275ae21d583a6"), "name" : "李四", "age" : 18, "isboy" : false, "score" : 70, "classname" : "软件一班" }
{ "_id" : ObjectId("628b721b642275ae21d583a7"), "name" : "王五", "age" : 19, "isboy" : true, "score" : 80, "classname" : "软件一班" }
{ "_id" : ObjectId("628b721b642275ae21d583a8"), "name" : "赵六", "age" : 20, "isboy" : false, "score" : 90, "classname" : "软件一班" }
{ "_id" : ObjectId("628b721b642275ae21d583a9"), "name" : "李白", "age" : 21, "isboy" : true, "score" : 85, "classname" : "软件二班" }
{ "_id" : ObjectId("628b721b642275ae21d583aa"), "name" : "杜甫", "age" : 22, "isboy" : false, "score" : 75, "classname" : "软件二班" }
{ "_id" : ObjectId("628b721b642275ae21d583ab"), "name" : "李逵", "age" : 23, "isboy" : true, "score" : 65, "classname" : "软件二班" }
{ "_id" : ObjectId("628b721b642275ae21d583ac"), "name" : "宋江", "age" : 24, "isboy" : false, "score" : 55, "classname" : "软件二班" }
{ "_id" : ObjectId("628b721b642275ae21d583ad"), "name" : "王维", "age" : 25, "isboy" : true, "score" : 50, "classname" : "网络一班" }
新建一个展示数据的方法
private static void FindShow(FindIterable增加一条数据documents){ //调用集合的find方法 MongoCursor iterator = documents.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } }
Document document = new Document("name", "田七").
append("age", 23.0).
append("isboy", true).
append("score", 80.0).
append("classname","网络一班");
ArrayList document1 = new ArrayList<>();
document1.add(document);
students.insertMany(document1);
FindIterable documents = students.find();
FindShow(documents);
运行结果
更新文档将文档中 classname="网络一班" 修改为 classname="计算机一班"
students.updateMany(Filters.eq("classname", "网络一班"),
new Document("$set",new Document("classname","计算机一班")));
System.out.println("-----更新文档 将文档中 classname=网络一班 修改为 classname=计算机一班-----");
documents = students.find();
FindShow(documents);
运行结果
查询文档查询软件一班的学生信息
documents = students.find(Filters.eq("classname", "软件一班"));
FindShow(documents);
运行结果
年龄小于22 的学生信息
documents = students.find(Filters.lt("age", 22));
FindShow(documents);
运行结果
多条件查询成绩小于60的女生的信息
documents=students.find(Filters.and(Filters.eq("isboy",false),Filters.lt("score",60)));
FindShow(documents);
运行结果
删除文档删除条件 classname=软件二班 的第一个文档
students.deleteOne(Filters.eq("classname", "软件二班"));
documents = students.find();
FindShow(documents);
运行结果
只删除了classname="软件二班"的第一条数据
删除条件 classname="软件二班" 的所有文档
students.deleteMany (Filters.eq("classname", "软件二班"));
documents = students.find();
FindShow(documents);
运行结果
由输出结果可见,集合中已没有 classname="软件二班" 的文档



