一、为两方建立起沟通的桥梁

 Java链接MongoDB

1--        导入MongoDB驱动包

2---        新建类,获取链接对象 

MongoClient  mongoClient = new MongoClient("localhost",27017);

3---        关闭链接

mongoClient.close();

二、如何查看数据库、集合 

1、查看链接MongoDB的数据库 

public class getDatas {
	public static void main(String[] args) {
		//获取Mongo的链接对象
		MongoClient  mongoClient = new MongoClient("localhost",27017);
		
		//获取库对象
		MongoDatabase database = mongoClient.getDatabase("myhahaha");
		
		//查看链接的MongoDB中的所有的库
		MongoIterable<String> listCollectionNames = database.listCollectionNames();
		for(String st:listCollectionNames) {
			System.out.println(st);
		}
	}
}

 在MongoDB中创建的数据库(如下图):

 2、使用MongoDB中的库查看库中的集合

public class GetCollectionDemo {
	public static void main(String[] args) {
		//获取Mongo的链接对象
		MongoClient  mongoClient = new MongoClient("localhost",27017);
		
		//获取库对象
		MongoDatabase mongoDatabase = mongoClient.getDatabase("myhahaha");
		
		//获取表对象
		MongoCollection<Document> collection = mongoDatabase.getCollection("student");
		
		//全查 Document文档的集合
		FindIterable<Document> find = collection.find();
		
		//迭代器对象  Cursor游标就是迭代器
		MongoCursor<Document> iterator = find.iterator();
		
		//遍历取值
		while(iterator.hasNext()) {
			Document next = iterator.next();
			System.out.println(next);
		}
		//关闭链接
		mongoClient.close();
	}
}

在MongoDB中创建的数据库(如下图): 

 

3、获取单独的某一个库的对象 和 库中所有的集合的操作(表)

public class GetCollectionDemo {

	public static void main(String[] args) {

		// 获取Mongo的链接对象
		MongoClient mc = new MongoClient("localhost", 27017);
		
		// 获取库对象
		MongoDatabase db = mc.getDatabase("myschool");
		
		System.out.println(db);
		
		// 获取库中的表的集合
		MongoIterable<String> listcollnames = db.listCollectionNames();
		
		// 表的迭代器对象
		MongoCursor<String> iterator = listcollnames.iterator();
		
		while(iterator.hasNext()) {
			System.out.println(iterator.next()); 
		}
		mc.close();
	}
}

 三、进行数据库的增删改查功能

【一】新增 

1、新增一条数据 

public class documentTest {
	public static void main(String[] args) {
		//创建一个类对象
		Student su = new Student();
		su.setSbirthday(new Date());
		su.setSclassid(007);
		su.setSid(2);
		su.setSname("阿里");
		su.setSsex("男");
		
		//获取Mongo的链接对象
		MongoClient  mongoClient = new MongoClient("localhost",27017);
		
		//获取库对象
		MongoDatabase mongoDatabase = mongoClient.getDatabase("myhahaha");
		
		//获取表对象
		MongoCollection<Document> table = mongoDatabase.getCollection("student");
		
		//创建一个document 文档 对象
		Document document = new Document();
		document.put("sid", su.getSid());
		document.put("sbirthday", su.getSbirthday());
		document.put("sclassid", su.getSclassid());
		document.put("sname", su.getSname());
		document.put("ssex", su.getSsex());
		
		//新增一条数据
		table.insertOne(document);
		
		//关闭链接
		mongoClient.close();
	}
}

2、新增多条数据 

public static void main(String[] args) {
	
		// 新建对象设置数值
		Student s = new Student();
		s.setBirthday(new Date());
		s.setClassid(100);
		s.setSid(1);
		s.setSname("张三");
		s.setSsex("男");
		
		// 获取Mongo的链接对象
		MongoClient mc = new MongoClient("localhost", 27017);
		// 获取库对象
		MongoDatabase db = mc.getDatabase("myschool");
		// 获取表对象
		MongoCollection<Document> table = 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());
		
		
		Document doc2 = new Document();
		doc2.put("sid", 2);
		doc2.put("sname", "李四");
		doc2.put("birthday", new Date());
		doc2.put("ssex", "女");
		doc2.put("classid", 200);
		
		Document doc3 = new Document();
		doc3.put("sid", 3);
		doc3.put("sname", "王五");
		doc3.put("birthday", new Date());
		doc3.put("ssex", "男");
		doc3.put("classid", 300);
		
        //创建集合
		List<Document> dlist = new ArrayList<Document>();
		dlist.add(doc);
		dlist.add(doc2);
		dlist.add(doc3);
		
		
		// 插入多条数据
		table.insertMany(dlist);

		// 释放资源
		mc.close();
	}

}

【二】 删除

1.删除单个数据

​
public class DeleteStudentDemo {

	public static void main(String[] args) {
		
		// Mongo操作
		// 获取Mongo的链接对象
		MongoClient mc = new MongoClient("localhost", 27017);
		// 获取库对象
		MongoDatabase db = mc.getDatabase("myschool");
		// 获取表对象
		MongoCollection<Document> table = db.getCollection("student");
		
		Bson eq = Filters.eq("sname", "哈哈");
		
		DeleteResult deleteOne = table.deleteOne(eq);

		if(deleteMany.getDeletedCount() > 0) {
			System.out.println("删除成功");
		}else {
			System.out.println("删除失败");
		}
		mc.close();
	}

}

​

2.删除多个数据 

public class DeleteStudentDemo {

	public static void main(String[] args) {
		
		// Mongo操作
		// 获取Mongo的链接对象
		MongoClient mc = new MongoClient("localhost", 27017);
		// 获取库对象
		MongoDatabase db = mc.getDatabase("myschool");
		// 获取表对象
		MongoCollection<Document> table = db.getCollection("student");
		
		Bson eq = Filters.eq("sname", "哈哈");
		
		DeleteResult deleteMany = table.deleteMany(eq);
		System.out.println(deleteMany.getDeletedCount());

		if(deleteMany.getDeletedCount() > 0) {
			System.out.println("删除成功");
		}else {
			System.out.println("删除失败");
		}
		mc.close();
	}
}

【三】修改 

1.修改一条数据 

public class UpdateStudent{

	public static void main(String[] args) {
		
		// Mongo操作
		// 获取Mongo的链接对象
		MongoClient mc = new MongoClient("localhost", 27017);
		// 获取库对象
		MongoDatabase db = mc.getDatabase("myschool");
		// 获取表对象
		MongoCollection<Document> table = db.getCollection("student");
		
		// 条件对象   where ssex = 男 and (classid = 1 or classid = 2)
		Bson eq = Filters.and(
					Filters.eq("ssex", "男"), 
					Filters.or(
							Filters.eq("classid", 1),
							Filters.eq("classid", 2)
							)
					);

		// 要修改的数据
		Document doc = new Document();
		doc.put("$set", new Document("sname","哈哈"));

		UpdateResult updateOne = table.updateOne(eq, doc);
		
		System.out.println(updateOne);
		
		mc.close();

	}
}

2.修改多条数据 

​
public class UpdateStudentDemo {

	public static void main(String[] args) {
		
		// Mongo操作
		// 获取Mongo的链接对象
		MongoClient mc = new MongoClient("localhost", 27017);
		// 获取库对象
		MongoDatabase db = mc.getDatabase("myschool");
		// 获取表对象
		MongoCollection<Document> table = db.getCollection("student");
		
		// 条件对象   where ssex = 男 and (classid = 1 or classid = 2)
		Bson eq = Filters.and(
					Filters.eq("ssex", "男"), 
					Filters.or(
							Filters.eq("classid", 1),
							Filters.eq("classid", 2)
							)
					);

		// 要修改的数据
		Document doc = new Document();
		doc.put("$set", new Document("sname","哈哈"));

		UpdateResult updateMany = table.updateMany(eq, doc);
		System.out.println(updateMany);
		
		mc.close();

	}
}

​

【四】查询 

1、排序查询 

​
public class FindStudentDemo {
	public static void main(String[] args) {
		
		// 获取Mongo的链接对象
		MongoClient mc = new MongoClient("localhost", 27017);
		// 获取库对象
		MongoDatabase db = mc.getDatabase("myschool");
		// 获取表对象
		MongoCollection<Document> table = db.getCollection("student");
		
                                   
                            //排序查询,1~表示升序;-1~表示降序
		Document document = new Document("classid",-1);
		
		FindIterable<Document> find = table.find().sort(document);
		
		for(Document doc : find) {
			System.out.println(doc);
		}
		mc.close();	
	}
}

​

2、使用正则表达式进行模糊查询

​
​
public class FindStudentDemo {
	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", "帅");
		
		FindIterable<Document> find = table.find(bson);
        
        MongoCursor<Document> iterator = find.iterator();
		
		for(Document doc : find) {
			System.out.println(doc);
		}
		
		mc.close();
		
	}

​

​

3、全查 

​
public class FindStudentDemo {
	public static void main(String[] args) {
		
		// 获取Mongo的链接对象
		MongoClient mc = new MongoClient("localhost", 27017);
		// 获取库对象
		MongoDatabase db = mc.getDatabase("myschool");
		// 获取表对象
		MongoCollection<Document> table = db.getCollection("student");

        FindIterable<Document> find = table.find();
		
        MongoCursor<Document> iterator = find.iterator();

		for(Document doc : find) {
			System.out.println(doc);
		}
		mc.close();
	}

​

4.分页查询 

public class FindStudentDemo {
	public static void main(String[] args) {
		
		// 获取Mongo的链接对象
		MongoClient mc = new MongoClient("localhost", 27017);
		// 获取库对象
		MongoDatabase db = mc.getDatabase("myschool");
		// 获取表对象
		MongoCollection<Document> table = db.getCollection("student");
		
        // 跳转,分页
		FindIterable<Document> bbs =table.find().skip(2).limit(3);

		MongoCursor<Document> iterator = bbs.iterator();
		
        for(Document doc : find) {
			System.out.println(doc);
		}
		
		mc.close();
		
	}
}

5.带条件查询 

public class FindStudentDemo {
	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 b1 = Filters.eq("Sname","张三");

        FindIterator<Document> bbs = table.find(b1)
		
		MongoCursor<Document> iterator = bbs.iterator();
		
		for(Document doc : find) {
			System.out.println(doc);
		}
		mc.close();
		
	}
}

6.带多个条件查询 

public class FindStudentDemo {
	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 b1 = Filters.and(
                           Filters.eq("Sname","张三"),
                           Filters.gte("Classid",100),
                             
                                );

        FindIterator<Document> bbs = table.find(b1)
		
		MongoCursor<Document> iterator = bbs.iterator();
		
		for(Document doc : find) {
			System.out.println(doc);
		}
		mc.close();
		
	}
}

 

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐