Java連接MongoDB的常用方法詳解
一、Java鏈接MongoDB
1. 導(dǎo)入Mongo驅(qū)動包

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");
//使用正則表達(dá)式進(jìn)行模糊查找
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());
}到此這篇關(guān)于Java連接MongoDB的常用方法詳解的文章就介紹到這了,更多相關(guān)Java連接MongoDB內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java利用LocalDate類實(shí)現(xiàn)日歷設(shè)計(jì)
java中做時間處理時一般會采用java.util.Date,但是相比于Date來說,還有更好的選擇--java.time.LocalDate。本文就來用LocalDate類實(shí)現(xiàn)日歷設(shè)計(jì),感興趣的可以動手嘗試一下2022-07-07
Spring MVC請求參數(shù)接收的全面總結(jié)教程
這篇文章主要給大家總結(jié)介紹了關(guān)于Spring MVC請求參數(shù)接收的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
在Window系統(tǒng)下安裝Netbeans9的方法
今天小編就為大家分享一篇關(guān)于在Window系統(tǒng)下安裝Netbeans9的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
Java instanceof關(guān)鍵字的的進(jìn)一步理解
這篇文章主要介紹了Java instanceof關(guān)鍵字的的進(jìn)一步理解,本文用一些實(shí)例講解了instanceof操作符的一些知識,需要的朋友可以參考下2015-03-03
Spring Boot整合Web項(xiàng)目常用功能詳解
這篇文章主要介紹了Spring Boot整合Web項(xiàng)目常用功能詳解,在Web應(yīng)用開發(fā)過程中,可以通過Spring Boot的Starter來將這些常用功能進(jìn)行整合與集中維護(hù),以達(dá)到開箱即用的目的。,需要的朋友可以參考下2019-06-06
Java函數(shù)式編程(一):你好,Lambda表達(dá)式
這篇文章主要介紹了Java函數(shù)式編程(一):你好,Lambda表達(dá)式,本文講解了新老函數(shù)式編程的一些變化,需要的朋友可以參考下2014-09-09
SpringBoot+Redis+Lua分布式限流的實(shí)現(xiàn)
本文主要介紹了SpringBoot+Redis+Lua分布式限流的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
關(guān)于通過Java連接mysql對反斜杠”\“轉(zhuǎn)義的測試詳解
這篇文章主要給大家介紹了關(guān)于通過Java連接mysql對反斜杠”\“轉(zhuǎn)義的測試的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家理解反斜杠”\“轉(zhuǎn)義具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06
深入理解spring boot異步調(diào)用方式@Async
Spring為任務(wù)調(diào)度與異步方法執(zhí)行提供了注解支持。通過在方法上設(shè)置@Async注解,可使得方法被異步調(diào)用。下面這篇文章主要給大家介紹了關(guān)于spring boot異步調(diào)用方式@Async的相關(guān)資料,需要的朋友可以參考下。2017-07-07

