Java如何连接 MongoDB

MongoDB于Java建立连接的步骤:

 第一步:导入Jar包并 builpath路径

第二步:获取连接对象

//获取连接对象  这个是资源需要释放
		MongoClient mc=new MongoClient("localhost",27017);

最后一步:释放资源

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

Java中对MongoDB的操作示例
 

   获取所有数据库的名字

 废弃方法      --获取所有数据库的名字
	List tableNamse=mc.getDatabaseNames();
	for (String dnames : tableNamse) {
		System.out.println(dnames);
	}
	
迭代器遍历
	MongoIterable dnames = mc.listDatabaseNames();
			MongoCursor iterator = dnames.iterator();
			while(iterator.hasNext()) {
				System.out.println(iterator.next());
			}	
	
   增强for循环遍历
			for (String dnamelist : dnames) {
				System.out.println(dnamelist);
			}

 获取数据库

MongoDatabase db = mc.getDatabase("shujuku");

通过建立的连接对象 mc 的getDatebase方法,寻找使用名为"javaee2309"的库,如果该库不存在则创建该库(名为"javaee2309"的数据库)

获取集合

 通过获取的数据库对象 db 来获取集合

MongoCollection col = db.getCollection("person");

获取集合,如果该集合不存在则创建该集合,

注意:若该集合不存在,想要创建集合,且不在此集合中插入数据,则该集合为虚拟集合,不会真正地创建,所以要真实地创建该集合,需要在后续对该集合进行插入数据的操作。

插入数据

通过获取集合创建的 col对象来进行增加操作

增加一条数据:
col.insertOne(new Document("name","李四"));
增加多条数据:
        Document doc1 = new Document();
		
        doc1.append("name", "张三");
		doc1.append("age", 88);
		doc1.append("sex", "男");
		doc1.append("bir", "1999-1-1");
		doc1.append("score", 100.1);
		
		Document doc2 = new Document();
		doc2.append("name", "李四");
		doc2.append("age", 30);
		doc2.append("sex", "女");
		doc2.append("bir", new Date());
		doc2.append("score", 10.8);
		
		Document doc3 = new Document();
		doc3.append("name", "王五");
		doc3.append("age", 16);
		doc3.append("sex", "女");
		doc3.append("bir", new Date());
		doc3.append("score", 80.5);

因为插入多条数据入参需要集合类型,所以将这些数据放入集合

	List doclist = new ArrayList();
		doclist.add(doc1);
		doclist.add(doc2);
		doclist.add(doc3);

插入多条数据

col.insertMany(doclist);

修改数据

MongoClient mc=new MongoClient("localhost",27017);
	 
	 MongoDatabase db = mc.getDatabase("myschool");

	 MongoCollection col = db.getCollection("student");
	 //db.student.update({'name':'老六'},{ 'age':20});
//	 	Bson eq=Filters.eq("name", "老六");
	 //组织条件
	  Bson gt = Filters.gt("age", 30);
	 
	 Document doc =new Document();
//	 	doc.append("$set", new Document("age",20));
	 	doc.append("$set",  new Document("age",18));
	  UpdateResult updateMany = col.updateMany(gt, doc);
//	 	UpdateResult updateOne = col.updateOne(eq, doc);
//	 	System.out.println(updateOne);
	  System.out.println(updateMany);
	 	mc.close();

删除数据

	    MongoClient mc=new MongoClient("localhost",27017);
		
		MongoDatabase db = mc.getDatabase("myschool");
		
		MongoCollection col = db.getCollection("student");
		
		DeleteResult deleteOne = col.deleteOne(new Document("name","张三"));
		
//		DeleteResult deleteMany = col.deleteMany(new Document("age","{$lte:20}"));
		System.out.println(deleteOne);
		mc.close();

查询数据

    MongoClient mc=new MongoClient("localhost",27017);
	MongoDatabase db = mc.getDatabase("myschool");
	MongoCollection col = db.getCollection("student");
	FindIterable find = col.find();
	for (Document document : find) {
		System.out.println(document);
	}
		mc.close();

带条件的查询

	MongoClient mc=new MongoClient("localhost",27017);
	MongoDatabase db = mc.getDatabase("myschool");
	MongoCollection col = db.getCollection("student");
	Document doc =new Document();
	FindIterable find =         
    col.find(Filters.and(Filters.eq("sex",false),Filters.lt("age", 50)));
	for (Document document : find) {
		System.out.println(document);
	}
	mc.close();

分页的实现

	MongoClient mc=new MongoClient("localhost",27017);
	MongoDatabase db = mc.getDatabase("myschool");
	MongoCollection col = db.getCollection("student");
	//模糊查询声明是一个正则表达式,此处不需要加 / /
//	 Bson bson = Filters.regex("name", "帅$");
//	 FindIterable find = col.find(bson);
//	 for (Document doc : find ) {
//		System.out.println(doc);
//	}
		FindIterable limit = col.find().skip(1).limit(3);
		for (Document document : limit) {
			System.out.println(document);
		}
	 mc.close();

//排序的实现
	MongoClient mc=new MongoClient("localhost",27017);
	MongoDatabase db = mc.getDatabase("myschool");
	MongoCollection col = db.getCollection("student");
	
	FindIterable find = col.find().sort(new Document("age", -1));
	for (Document doc : find) {
	System.out.println(doc);	
	}
	 mc.close();

模糊查询的语法 -regex

    MongoClient mc=new MongoClient("localhost",27017);
	MongoDatabase db = mc.getDatabase("myschool");
	MongoCollection col = db.getCollection("student");
	//模糊查询声明是一个正则表达式,此处不需要加 / /
	 Bson bson = Filters.regex("name", "^王");
	 FindIterable find = col.find(bson);
	 for (Document doc : find ) {
		System.out.println(doc);
	}
	 mc.close();
    MongoClient mc=new MongoClient("localhost",27017);
	MongoDatabase db = mc.getDatabase("myschool");
	MongoCollection col = db.getCollection("student");
	//模糊查询声明是一个正则表达式,此处不需要加 / /
	 Bson bson = Filters.regex("name", "帅$");
	 FindIterable find = col.find(bson);
	 for (Document doc : find ) {
		System.out.println(doc);
	}
	 mc.close();

你可能感兴趣的:(mongodb,数据库)