溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java連接MongoDB的常用方法實例分析

發(fā)布時間:2022-07-19 09:23:29 來源:億速云 閱讀:200 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“Java連接MongoDB的常用方法實例分析”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Java連接MongoDB的常用方法實例分析”吧!

一、Java鏈接MongoDB

1. 導(dǎo)入Mongo驅(qū)動包

Java連接MongoDB的常用方法實例分析

2. 獲取Mongo鏈接對象

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

3. 關(guān)閉鏈接

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. 添加數(shù)據(jù)

a. 添加一條數(shù)據(jù)

//創(chuàng)建對象
Student s = new Student();
s.setSid(1);
s.setSname("王俊凱");
s.setBirthday(new Date());
s.setSsex("男");
s.setClassid(2);
 
//將數(shù)據(jù)轉(zhuǎn)換為json格式
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
String json = gson.toJson(s);
 
//獲取集合對象
MongoCollection<Document> collection = db.getCollection("student");
 
//添加一條數(shù)據(jù),將json格式轉(zhuǎn)換為document對象
collection.insertOne(Document.parse(json));

b. 添加多條數(shù)據(jù)

//存入數(shù)據(jù)
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);
    //將數(shù)據(jù)轉(zhuǎn)換為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");
 
//添加多條數(shù)據(jù)
collection.insertMany(dlist);

2. 刪除數(shù)據(jù)

a. 刪除一條數(shù)據(jù)

//獲取集合對象
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. 刪除多條數(shù)據(jù)

//獲取集合對象
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. 修改數(shù)據(jù)

a. 修改一條數(shù)據(jù)

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

b. 修改多條數(shù)據(jù)

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

4. 查詢數(shù)據(jù)

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的常用方法實例分析”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Java連接MongoDB的常用方法實例分析這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI