JAVA操作MongoDB的方法

一、JAVA链接MongoDB

  • 导入MongoDB驱动

                       

  • 获取链接对象
    MongoClient mc = new MongoClient("localhost",27017);
  • 关闭链接
    mc.close();

二、JAVA操作MongoDB的方法 

1.查看链接的MongoDB中的所有库

	MongoIterable<String> dblist=MongoClient.listDatabaseNames();
		
        for(String db:dblist) {
			System.out.println(db);
		}
		

2.使用库查看库中的集合

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

MongoIterable<String> dblist=MongoClient.listDatabaseNames();
		
        for(String db:dblist) {
			System.out.println(db);
		}

3.插入一条数据

        //存入MongoDB的数据
		Comment com=new comment();
		com.setContent("一二三");
		com.setPublishtime(new Date());
		//将数据转为json模式
		Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		String json = gson.toJson(com);
		
		//获取集合对象
		MongoCoolection<Document> coomCol=bbsDB.getCollection("comment");
		
		//添加一条数据,将json格式转换为document对象
		commCol.insertone(Document.parse(json));

4.插入多条数据

        //存入MongoDB的数据
		List<Document> dlist = new ArrayList<Document>();
		
		// 需要的数据
		for(int i=0;i<5;i++) {
			com.setId(Integer.toString(i+1));
			com.setContent("一二三");
			 
			//将数据转换为json模式
			Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
			String json=gson.toJson(com);
			dlist.add(Document.parse(json));
		}
		
		//获取集合对象
		MongoCollection<Document> commCol =bbsDB.getCollection("comment");
		
		//添加多条数据
		commCol.insertMany(dlist);

5.删除一条数据

        // 删除数据
	  MongoCollection<Document> commCol= db.getCollection("myschool");
	
	  Comment com=new Comment();
	  com.setId("1");
	  
	
		Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		
		Bson bson=Document.parse(gson.toJson(com));
		
		DeleteResult one=commCol.deleteOne(bson);
		
		if(one.getDeletedCount()>0) {
			System.out.println("删除成功");
		}else {
			System.out.println("删除失败");
		}

        mongoClient.close();

6.删除多条数据

      // 删除数据
	  MongoCollection<Document> commCol= db.getCollection("myschool");
	
	  Comment com=new Comment();
	  com.setId("1");
	  
	
		Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		
		Bson bson=Document.parse(gson.toJson(com));
		
		DeleteResult many=commCol.deleteMany(bson);
		
		if(many.getDeletedCount()>0) {
			System.out.println("删除成功");
		}else {
			System.out.println("删除失败");
		}
        mongoClient.close();

7.修改一条数据

     MongoCollection<Document> table = db.getCollection("student");
		  
		  // 条件对象   where ssex = 男 and (classid = 1 or classid = 2)
		  Bson b=Filters.eq("content","一二三");
		 
		  // 要修改的数据
		  Document doc = new Document();
		  doc.setLikenum(1);
		 Bson b1=new Document("$set",Document.parse(gson.toJson(doc)))
		 
		  UpdateResult updateOne = table.updateOne(b, b1);
		  
		 if(updateOne.getModifiedCount()>0) {
			 System.out.println("修改成功");
		 }else {
			 System.out.println("修改失败");
		 }
        mongoClient.close();

8.修改多条数据

     MongoCollection<Document> table = db.getCollection("student");
		  
		  // 条件对象   where ssex = 男 and (classid = 1 or classid = 2)
		  Bson b=Filters.eq("content","一二三");
		 
		  // 要修改的数据
		  Document doc = new Document();
		  doc.setLikenum(1);
		 Bson b1=new Document("$set",Document.parse(gson.toJson(doc)))
		 
		  UpdateResult updateMany = table.updateMany(b, b1);
		  
		 if(updateMany.getModifiedCount()>0) {
			 System.out.println("修改成功");
		 }else {
			 System.out.println("修改失败");
		 }

    mongoClient.close();

9.全查询

MongoCollection<Document> commCol=bbsDB.getCollection("comment");
    FindTterable<Document> bses=commCol.find();
	
	MongoCursor<Document> i=bbses.iterator();
	while(i.hasNext()) {
		System.out.println(i.next());
	}
	
	mongoClient.close();

10.带条件查询

	MongoCollection<Document> commCol=bbsDB.getCollection("comment");
	
	Bson b1=Filters.eq("content","一二三");
	
	FindTterable<Document> bses=commCol.find(b1);
	
	MongoCursor<Document> i=bbses.iterator();
	while(i.hasNext()) {
		System.out.println(i.next());
	}
	
	mongoClient.close();

11.模糊查询

MongoCollection<Document> commCol=bbsDB.getCollection("comment");
	
	//用正则表达式进行模糊查找
	Bson b1=Filters.regex("content","一二三");
	
	FindTterable<Document> bses=commCol.find(b1);
	
	MongoCursor<Document> i=bbses.iterator();
	while(i.hasNext()) {
		System.out.println(i.next());
	}
	
	mongoClient.close();

12.分页查询

MongoCollection<Document> commCol=bbsDB.getCollection("comment");
	
	//用正则表达式进行模糊查找
	Bson b1=Filters.regex("content","一二三");
	
	FindTterable<Document> bses=commCol.find().skip(2).limit(3);
	
	MongoCursor<Document> i=bbses.iterator();
	while(i.hasNext()) {
		System.out.println(i.next());
	}
	
	mongoClient.close();

13.排序查询

	MongoCollection<Document> commCol=bbsDB.getCollection("comment");
	
	//排序1表示升序,-1表示降序
	Bson b1=new Document("id",-1);
	
	FindTterable<Document> bses=commCol.find().sort(b1);
	
	MongoCursor<Document> i=bbses.iterator();
	while(i.hasNext()) {
		System.out.println(i.next());
	}
	
	mongoClient.close();

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值