环境配置
想要在 Java 程序中使用 MongoDB,需要确保您的电脑上已经安装了 MongoDB,并搭建好了 Java 的环境。您可以查看《Java教程》来了解不同系统下 Java 的安装教程。
另外您还需要确认安装了 MongoDB JDBC 驱动,您可以通过 http://mongodb.github.io/mongo-java-driver/来下载最新版的 mongodb-driver-3.11.2.jar 以及其依赖项 mongodb-driver-core-3.11.2.jar。
获取数据库中的集合
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
public class GetCollection {
public static void main(String[] args) {
//获取mongo链接对象
MongoClient mc = new MongoClient("localhost", 27017);
//获取库对象
MongoDatabase db = mc.getDatabase("myschool");
//获取表集合
MongoIterable<String> names = db.listCollectionNames();
MongoCursor<String> iterator = names.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
查询
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class FindStudent {
public static void main(String[] args) {
// 获取Mongo的链接对象
MongoClient mc = new MongoClient("localhost", 27017);
// 获取库对象
MongoDatabase db = mc.getDatabase("myschool");
// 获取表对象
MongoCollection<Document> table = db.getCollection("student");
// Bson bson = Filters.regex("sname", "帅");
Document document = new Document("classid",-1);
// FindIterable<Document> find = table.find(bson);
FindIterable<Document> find = table.find().sort(document);
//MongoCursor<Document> iterator = find.iterator();
for(Document doc : find) {
System.out.println(doc);
}
mc.close();
}
}
新增
import java.util.Date;
import org.bson.Document;
import com.ape.bean.Student;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class AddStudent {
public static void main(String[] args) {
Student s = new Student();
s.setSid(1);
s.setSname("张大大");
s.setBirthday(new Date());
s.setSsex("男");
s.setClassid(20);
//获取mongo链接对象
MongoClient mc = new MongoClient("localhost", 27017);
//获取库对象
MongoDatabase db = mc.getDatabase("myschool");
//获取表对象
MongoCollection<Document> collection = db.getCollection("student");
//创建Document对象
Document doc = new Document();
doc.put("sid", s.getSid());
doc.put("sname", s.getSname());
doc.put("birthday", s.getBirthday());
doc.put("ssex", s.getSsex());
doc.put("classid", s.getClassid());
collection.insertOne(doc);
mc.close();
}
}
修改
import org.bson.Document;
import org.bson.conversions.Bson;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.UpdateResult;
public class UpdateStudent {
public static void main(String[] args) {
//获取mongo链接对象
MongoClient mc = new MongoClient("localhost", 27017);
//获取库对象
MongoDatabase db = mc.getDatabase("myschool");
//获取表对象
MongoCollection<Document> collection = db.getCollection("student");
// Bson eq = Filters.eq("name", "王五");
Bson eq = Filters.and(Filters.eq("ssex", "女"),
Filters.or(Filters.gte("age", 18),Filters.lte("age", 30)));
//要修改的数据
Document doc = new Document();
doc.put("$set", new Document("name","凌云"));
UpdateResult result = collection.updateOne(eq, doc);
System.out.println(result);
mc.close();
}
}
删除
import org.bson.Document;
import org.bson.conversions.Bson;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.DeleteResult;
public class DeleteStudent {
public static void main(String[] args) {
//获取mongo链接对象
MongoClient mc = new MongoClient("localhost", 27017);
//获取库对象
MongoDatabase db = mc.getDatabase("myschool");
//获取表对象
MongoCollection<Document> table = db.getCollection("student");
Bson eq = Filters.eq("name", "王五");
DeleteResult deleteOne = table.deleteOne(eq);
System.out.println(deleteOne);
if(deleteOne.getDeletedCount()>0) {
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
}