一·、java连接mongodb
1.先导入Mongodb驱动jar包
2.创建链接对象
MongoClient mc = new MongoClient("localhost",27017);
3.关闭资源
mc.close();
二、Java操作mongodb增删改查操作
1.1增加数据(一条数据)
MongoClient mc = new MongoClient("localhost",27017);
//拿到数据库
MongoDatabase db = mc.getDatabase("myschool");
//拿到集合
MongoCollection<Document> collection = db.getCollection("student");
//创建document对象并向其添加值
Document document = new Document();
document.put("name", "张三");
document.put("sex", "男");
document.put("age", 18);
document.put("birthday", new Date());
collection.insertOne(document);
mc.close();
1.2增加多条数据
MongoClient mc = new MongoClient("localhost",27017);
//拿到数据库
MongoDatabase db = mc.getDatabase("myschool");
//拿到集合
MongoCollection<Document> collection = db.getCollection("student");
//创建document对象并向其添加值
Document document = new Document();
document.put("name", "张三");
document.put("sex", "男");
document.put("age", 18);
document.put("birthday", new Date());
Document document2 = new Document();
document2.put("name", "张三");
document2.put("sex", "男");
document2.put("age", 18);
document2.put("birthday", new Date());
Document document3 = new Document();
document3.put("name", "张三");
document3.put("sex", "男");
document3.put("age", 18);
document3.put("birthday", new Date());
//创建array list集合 将所有doucument对象加入
List<Document> listdoc = new ArrayList<Document>();
listdoc.add(document);
listdoc.add(document2);
listdoc.add(document3);
collection.insertMany(listdoc);
mc.close();
2.1删除数据(一条)
MongoClient mc = new MongoClient("localhost",27017);
MongoDatabase db = mc.getDatabase("myschool");
MongoCollection<Document> collection = db.getCollection("student");
Bson b = new Document("name","张三");
DeleteResult deleteOne = collection.deleteOne(b);
System.out.println(deleteOne);
System.out.println(deleteMany);
mc.close();
2.2删除数据(多条(根据条件删除))
MongoClient mc = new MongoClient("localhost",27017);
MongoDatabase db = mc.getDatabase("myschool");
MongoCollection<Document> collection = db.getCollection("student");
// Bson exists = Filters.exists("age");
Bson exists = Filters.exists("age",false);
DeleteResult deleteMany = collection.deleteMany(exists);
System.out.println(deleteMany);
mc.close();
3.1修改数据(一条)
MongoClient mc = new MongoClient("localhost",27017);
MongoDatabase db = mc.getDatabase("myschool");
MongoCollection<Document> collection = db.getCollection("student");
Bson eq = Filters.eq("name", "张三");
UpdateResult updateOne = collection.updateOne(eq, new Document("$set",new Document("age",22)));
System.out.println(updateOne);
mc.close();
3.2修改数据(多条(根据条件修改))
MongoClient mc = new MongoClient("localhost",27017);
MongoDatabase db = mc.getDatabase("myschool");
MongoCollection<Document> collection = db.getCollection("student");
Bson and = (Filters.and(Filters.gte("age", 120),Filters.lte("age", 130)));
UpdateResult updateMany = collection.updateMany(and,new Document("$inc",new Document("age",-100)));
System.out.println(updateMany);
mc.close();
4.查找数据
MongoClient mc = new MongoClient("localhost",27017);
MongoDatabase db = mc.getDatabase("myschool");
MongoCollection<Document> collection = db.getCollection("student");
FindIterable<Document> find = collection.find();
for (Document document : find) {
System.out.println(document);
}
mc.close();