6.30Java连接MongoDB进行操作练习

6.30Java连接MongoDB进行操作练习

代码

封装类:

package mongodbtest;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;

/**
* 使用java连接mongodb并且进行更新文档的测试代码
* 要求:
* 对连接步骤进行方法的封装:
* 1、连接数据库方法
* 2、创建集合方法
* 3、获取到集合方法
* 4、插入文档方法
* 5、查看文档方法
* 6、更新文档方法
* @since JDK 1.8
* @date 2021/6/30
* @author Lucifer
*/
public class MongoDB {

   /*定义连接需要用到的属性*/
   private static String MONGO_HOST = "localhost";
   private static Integer MONGO_PORT = 27017;
   private static final String MONGO_DB_NAME = "practice";
   private static final String GATHER_NAME = "practiceDemo";

   /**
    * 属性设置方法
    * @return
    */
   public static String getMongoHost() {
       return MONGO_HOST;
  }
   public static Integer getMongoPort() {
       return MONGO_PORT;
  }
   public static void setMongoHost(String mongoHost) {
       MONGO_HOST = mongoHost;
  }
   public static void setMongoPort(Integer mongoPort) {
       MONGO_PORT = mongoPort;
  }

   /**
    * 连接数据库方法
    * @Test
    */
   public static MongoDatabase connect(String mongo_host, Integer mongo_port){

       //连接到mongodb服务--->使用Mongo客户端类创建连接对象
       MongoClient mongoClient = new MongoClient(mongo_host,mongo_port);

       //连接到数据库--->使用mongo数据库类调用mongodb服务对象通过方法获取
       MongoDatabase mongoDatabase = mongoClient.getDatabase(MONGO_DB_NAME);
       System.out.println("Connect to database successfully!!!");

       //返回mongodatabase对象
       return mongoDatabase;

  }

   /**
    * 创建集合方法
    * @Create
    * @return
    */
   public static String create(){

//       //连接到mongodb服务--->创建类对象,操作类方法获取数据库信息
//       MongoDB mdb = new MongoDB();
//
//       //连接数据库
//       MongoDatabase mongoDatabase = mdb.connect(MONGO_HOST,MONGO_PORT);
       MongoDatabase mongoDatabase = MongoDB.connect(MONGO_HOST,MONGO_PORT);

       //创建集合--->使用mongoDatabase的引用创建集合
       mongoDatabase.createCollection(GATHER_NAME);
       System.out.println("集合创建成功");

       return null;

  }

   /**
    * 获取集合方法
    * 使用到新类,返回新类的对象
    * @getCollection
    */
   public static MongoCollection<Document> getCollection(){

//       //连接到mongodb服务
//       MongoDB mdb = new MongoDB();
//
//       //调用类方法获取数据库返回值对象
//       MongoDatabase mongoDatabase = mdb.connect(MONGO_HOST,MONGO_PORT);
       MongoDatabase mongoDatabase = MongoDB.connect(MONGO_HOST,MONGO_PORT);

       //使用MongoCollection类获取文档对象信息
       MongoCollection<Document> collection = mongoDatabase.getCollection(GATHER_NAME);
       System.out.println("集合选择成功!!!");

       return collection;

  }

   /**
    * 往集合当中插入三条数据
    * @insert
    */
   public static void insert(){

       //连接数据库、创建集合、获取集合信息
       MongoCollection<Document> collection = getCollection();

       /*
       1、创建三个文档对象
       2、使用List列表数据结构将它们存储起来
       3、将文档列表插入数据库集合中--->列表插入集合 mongoCollection.insertMany(List<Document>)
        */
       Document document1 = new Document("title","Redis").
               append("description","Redis是一个很好的缓存数据库").
               append("by","JunBoy").
               append("tags","['redis']").
               append("like",200);
       Document document2 = new Document("title","Java").
               append("description","Java是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。").
               append("by","JunBoy").
               append("tags","['java']").
               append("like",150);
       Document document3 = new Document("title","MongoDB").
               append("description","MongoDB 是一个 Nosql 数据库").
               append("by","JunBoy").
               append("tags","['mongodb']").
               append("like",100);

       /*创建List列表*/
       List<Document> documents = new ArrayList<Document>();
       documents.add(document1);
       documents.add(document2);
       documents.add(document3);

       //使用MongoClient类的insertMany方法插入列表
       collection.insertMany(documents);
       System.out.println("文档插入成功");
  }
}

调用类:

package mongodbtest;

public class MongoDBTest {
   public static void main(String[] args) {
     MongoDB.create();
       MongoDB.insert();
  }
}

 

It's a lonely road!!!
原文地址:https://www.cnblogs.com/JunkingBoy/p/14955961.html