目录

一、什么是MongoDB?

二、MongoDB基本操作

1.产看所有库

2.创建库

3.查看当前库

4.删除库(危险操作!一般不使用)

 三.java操作MogoDB

1.新增

2.删除

3.修改

4.查询



一、什么是MongoDB?

        MongoDB 是一个基于分布式文件存储的数据库。它是一个介于关系型数据库和非关系型数据库之间的产品,是非关系型数据库当中功能最丰富也是最像关系型数据库的。

        MongDB优点:1.有很强的的扩展性;

                                 2.支持多种编程语言;

                                 3.面向文档存储,操作比较简单;

        缺点:1.不支持事务;    2.不能进行多表联查;

二、MongoDB基本操作

1.产看所有库

show dbs;

2.创建库

use 库名;
注:如果库中没有数据,就会是一个虚拟库,查看库的时候不会显示该库

3.查看当前库

db;

4.删除库(危险操作!一般不使用)

db.dropDatabase();

 三.java操作MogoDB

        在java中使用mongoDB之前,首先需要java连接mongoDB的第三方驱动包(通过在xml文件中添加依赖)

1.新增

public class AddDemo {
	public static void main(String[] args) {
		//获取链接
		MongoClient mc = new MongoClient("localhost",27017);
		//获取库对象
		MongoDatabase db = mc.getDatabase("student");
		//获取集合对象
		MongoCollection<Document> collection = db.getCollection("student");
		//新增
		Document document = new Document();
		document.put("name", "周子舒");
		document.put("sex", "男");
		document.put("age", 18);
		document.put("birthday",new Date());
		
		//添加一条数据
		collection.insertOne(document);
		
		//添加多条数据
		Document document1= new Document();
		document1.put("name", "钟无艳");
		document1.put("sex", "女");
		document1.put("age", 18);
		document1.put("birthday",new Date());
		
		Document document2= new Document();
		document2.put("name", "貂蝉");
		document2.put("sex", "女");
		document2.put("age", 1);
		document2.put("birthday",new Date());
		
		ArrayList<Document> list = new ArrayList<Document>();
		list.add(document1);
		list.add(document2);
		collection.insertMany(list);
		mc.close();
	}
}

2.删除

         删除某条单个数据时,使用 MongoCollection 对象的 deleteOne() 方法,该方法接收一个数据类型为 Bson 的的对象作为过滤器筛选出需要删除的文档。注意deleteOne() 方法只会删除表中满足删除条件的第一条数据。JDBC驱动程序提供了 Filters 类来为所有的MongoDB查询操作提供静态方法。每个方法返回的BSON类型。


public void deleteOneTest(){
    public static void main(String[] args) {
        //获取链接
		MongoClient mc = new MongoClient("localhost",27017);
		//获取库对象
		MongoDatabase db = mc.getDatabase("student");
		//获取集合对象
		MongoCollection<Document> collection = db.getCollection("student");
        //删除条件
        Bson filter = Filters.eq("age",18);
        //删除与筛选器匹配的单个文档
        collection.deleteOne(filter);
        mc.close();
    }
}





       
 Filters.eq()   //匹配到等于指定值的数据
 Filters.gt()   //匹配到大于指定值的数据
 Filters.gte()   //匹配到大于等于定值的数据
 Filters.lt()   //匹配到小于指定值的数据

删除多条数据, 使用 MongoCollection 对象的 deleteMany() 方法,该方法会将匹配到的所有数据全部删除。

public class DeleteDemo {
	public static void main(String[] args) {
		//获取链接
		MongoClient mc = new MongoClient("localhost",27017);
		//获取库对象
		MongoDatabase db = mc.getDatabase("student");
		//获取集合对象
		MongoCollection<Document> collection = db.getCollection("student");
//		Bson exists=Filters.exists("age",false);
		Bson gt=Filters.gt("age", 100);
//		Bson age=Filters.exists("age");
		DeleteResult deleteMany = collection.deleteMany(gt);
		System.out.println(deleteMany);
		mc.close();
	}
}

3.修改

        修改单个数据,使用 MongoCollection 对象的 updateOne() 方法,该方法接收两个参数,第一个数据类型为 Bson 的过滤器筛选出需要修改的文档,第二个参数数据类型为 Bson 指定如何修改筛选出的文档。然后修改过滤器筛选出的第一个文档。

        修改多条数据,使用 MongoCollection 对象的 updateMany() 方法,也要接受两个参数,第一个是修改条件,第二是修改后的数据。

public class UpdateDemo {
	public static void main(String[] args) {
		//获取链接
				MongoClient mc = new MongoClient("localhost",27017);
				//获取库对象
				MongoDatabase db = mc.getDatabase("student");
				//获取集合对象
				MongoCollection<Document> collection = db.getCollection("student");
	//			Bson eq = Filters.eq("name","吕布");
				//修改一条数据
//				UpdateResult updateOne = collection.updateOne(eq, new Document("$set",new Document("age",99)),new UpdateOptions().upsert(true));
//				System.out.println(updateOne);
				
				//修改多条数据
				Bson and=Filters.and(Filters.gt("age",20),Filters.lte("age", 100));
//				UpdateResult updateMany = collection.updateMany(eq, new Document("$set",new Document("age",3)));
				UpdateResult updateMany = collection.updateMany(and, new Document("$inc",new Document("age",100)));
				System.out.println(updateMany);
				mc.close();
	}
}

4.查询

        查询方式较多,分别为:

         1.模糊查询

Bson b=Filters.regex("name","张");//查出所有姓张的数据

        2.分页查

FindIterable<Document> b=collection.find().skip(0).limit(3);//跳过第0条数据,一次看三条数据

       3.排序查询

Bson b=new Document("id",-1);//根据id倒叙排序
FindIterable<Document> d=collection.find().sort(b);
public class SelectDemo {
	public static void main(String[] args) {
		//获取链接
		MongoClient mc = new MongoClient("localhost",27017);
		//获取库对象
		MongoDatabase db = mc.getDatabase("student");
		//获取集合对象
		MongoCollection<Document> collection = db.getCollection("student");
		
		// 添加条件
				Bson eq = Filters.regex("name", "张");
				Document document = new Document("birthday",-1);
				// .limit(2).skip(2)
				FindIterable<Document> find = collection.find(eq).sort(document);
				List<Student>  slist = new ArrayList<Student>();
				
				MongoCursor<Document> iterator = find.iterator();
				
				
				while(iterator.hasNext()) {
					Student s =  new Student();
					
					Document next = iterator.next();
					s.setSname(next.getString("name"));
					s.setSsex(next.getString("sex"));
					s.setSid(next.getInteger("sid"));
					
					// 参数1 Json 字符串
					// 参数2 需要的对象的类型
					
//					String json = next.toJson();
//					System.out.println(json);
//					Student s = gson.fromJson(json, Student.class);
					
					slist.add(s);
				}
				
				
				for(Student ss : slist){
					System.out.println(ss);
				}

				mc.close();
				
		
	}
}

        


Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐