首頁 > 軟體

Java連線MongoDB的常用方法詳解

2022-07-18 22:05:11

一、Java連結MongoDB

1. 匯入Mongo驅動包

2. 獲取Mongo連結物件

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

3. 關閉連結

mc.close();

二、檢視庫,檢視集合

1. 獲取庫物件

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

2. 獲取庫中表的集合

MongoIterable<String> listCollectionNames = db.listCollectionNames();
        
MongoCursor<String> iterator = listCollectionNames.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }

三、Java對MongoDB增刪改查

1. 新增資料

a. 新增一條資料

//建立物件
Student s = new Student();
s.setSid(1);
s.setSname("王俊凱");
s.setBirthday(new Date());
s.setSsex("男");
s.setClassid(2);
 
//將資料轉換為json格式
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
String json = gson.toJson(s);
 
//獲取集合物件
MongoCollection<Document> collection = db.getCollection("student");
 
//新增一條資料,將json格式轉換為document物件
collection.insertOne(Document.parse(json));

b. 新增多條資料

//存入資料
List<Document> dlist=new ArrayList<Document>();
 
for(int i=0; i<3; i++){
    Student s = new Student();
    s.setSid(Integer.toString(i+1));
    s.setSname("王源");
    s.setBirthday(new Date());
    s.setSsex("男");
    s.setClassid(1);
    //將資料轉換為json格式
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
    String json = gson.toJson(s);
    dlist.add(Document.parse(json));
}
 
//獲取集合物件
MongoCollection<Document> collection = db.getCollection("student");
 
//新增多條資料
collection.insertMany(dlist);

2. 刪除資料

a. 刪除一條資料

//獲取集合物件
MongoCollection<Document> collection = db.getCollection("student");
 
Student s = new Student();
s.setSid(1);
 
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
Bson bson = Document.parse(gson.toJson(s));
 
DeleteResult deleteOne = collection.deleteOne(bson);

b. 刪除多條資料

//獲取集合物件
MongoCollection<Document> collection = db.getCollection("student");
 
Student s = new Student();
s.setSname("王源");
 
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
Bson bson = Document.parse(gson.toJson(s));
 
DeleteResult deleteMany = collection.deleteMany(bson);

3. 修改資料

a. 修改一條資料

MongoCollection<Document> collection = db.getCollection("student");
 
//一個條件物件
Bson eq = Filters.eq("sname","易烊千璽");
 
//要修改的資料
Document doc = new Document();
doc.put("$set", new Document("age",22));
UpdateResult  updateone = collection.updateOne(eq, doc);
System.out.println(updateone);

b. 修改多條資料

MongoCollection<Document> collection = db.getCollection("student");
 
//多條件
Bson bson = Filters.and(Filters.gte("age", 20),Filters.lte("age", 40));
        
//要修改的資料
Document doc = new Document();        
doc.put("$set", new Document("sex","男"));
UpdateResult updateMany = collection.updateMany(bson, doc);
System.out.println(updateMany);

4. 查詢資料

a. 全查

MongoCollection<Document> collection = db.getCollection("student");
 
FindIterable<Document> findAll = collection.find();
 
MongoCursor<Document> iterator = findAll.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

b. 帶條件查詢

MongoCollection<Document> collection = db.getCollection("student");
 
//一個條件物件
Bson eq = Filters.eq("sname","易烊千璽");
 
FindIterable<Document> findOne = collection.find(eq);
 
MongoCursor<Document> iterator = findOne.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

c. 模糊查詢

MongoCollection<Document> collection = db.getCollection("student");
 
//使用正規表示式進行模糊查詢
Bson eq = Filters.regex("sname","易");
 
FindIterable<Document> find = collection.find(eq);
 
MongoCursor<Document> iterator = find.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

d. 分頁查詢

MongoCollection<Document> collection = db.getCollection("student");
 
//分頁查詢
FindIterable<Document> find = collection.find().skip(2).limit(3);
 
MongoCursor<Document> iterator = find.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

e. 排序查詢

MongoCollection<Document> collection = db.getCollection("student");
 
//排序查詢  1升序   -1降序
Bson bson = new Document("sid",1);
FindIterable<Document> find = collection.find().sort(bson);
 
MongoCursor<Document> iterator = find.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

到此這篇關於Java連線MongoDB的常用方法詳解的文章就介紹到這了,更多相關Java連線MongoDB內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com