Java連接MongoDB的常用方法詳解
一、Java鏈接MongoDB
1. 導(dǎo)入Mongo驅(qū)動(dòng)包
2. 獲取Mongo鏈接對(duì)象
MongoClient mc = new MongoClient("localhost",27017);
3. 關(guān)閉鏈接
mc.close();
二、查看庫(kù),查看集合
1. 獲取庫(kù)對(duì)象
MongoDatabase db = mc.getDatabase("myschool");
2. 獲取庫(kù)中表的集合
MongoIterable<String> listCollectionNames = db.listCollectionNames(); MongoCursor<String> iterator = listCollectionNames.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }
三、Java對(duì)MongoDB增刪改查
1. 添加數(shù)據(jù)
a. 添加一條數(shù)據(jù)
//創(chuàng)建對(duì)象 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); //獲取集合對(duì)象 MongoCollection<Document> collection = db.getCollection("student"); //添加一條數(shù)據(jù),將json格式轉(zhuǎn)換為document對(duì)象 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)); } //獲取集合對(duì)象 MongoCollection<Document> collection = db.getCollection("student"); //添加多條數(shù)據(jù) collection.insertMany(dlist);
2. 刪除數(shù)據(jù)
a. 刪除一條數(shù)據(jù)
//獲取集合對(duì)象 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ù)
//獲取集合對(duì)象 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"); //一個(gè)條件對(duì)象 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. 查詢(xún)數(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. 帶條件查詢(xún)
MongoCollection<Document> collection = db.getCollection("student"); //一個(gè)條件對(duì)象 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. 模糊查詢(xún)
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. 分頁(yè)查詢(xún)
MongoCollection<Document> collection = db.getCollection("student"); //分頁(yè)查詢(xún) FindIterable<Document> find = collection.find().skip(2).limit(3); MongoCursor<Document> iterator = find.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); }
e. 排序查詢(xún)
MongoCollection<Document> collection = db.getCollection("student"); //排序查詢(xún) 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java利用LocalDate類(lèi)實(shí)現(xiàn)日歷設(shè)計(jì)
java中做時(shí)間處理時(shí)一般會(huì)采用java.util.Date,但是相比于Date來(lái)說(shuō),還有更好的選擇--java.time.LocalDate。本文就來(lái)用LocalDate類(lèi)實(shí)現(xiàn)日歷設(shè)計(jì),感興趣的可以動(dòng)手嘗試一下2022-07-07Spring MVC請(qǐng)求參數(shù)接收的全面總結(jié)教程
這篇文章主要給大家總結(jié)介紹了關(guān)于Spring MVC請(qǐng)求參數(shù)接收的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08在Window系統(tǒng)下安裝Netbeans9的方法
今天小編就為大家分享一篇關(guān)于在Window系統(tǒng)下安裝Netbeans9的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12Java instanceof關(guān)鍵字的的進(jìn)一步理解
這篇文章主要介紹了Java instanceof關(guān)鍵字的的進(jìn)一步理解,本文用一些實(shí)例講解了instanceof操作符的一些知識(shí),需要的朋友可以參考下2015-03-03Spring Boot整合Web項(xiàng)目常用功能詳解
這篇文章主要介紹了Spring Boot整合Web項(xiàng)目常用功能詳解,在Web應(yīng)用開(kāi)發(fā)過(guò)程中,可以通過(guò)Spring Boot的Starter來(lái)將這些常用功能進(jìn)行整合與集中維護(hù),以達(dá)到開(kāi)箱即用的目的。,需要的朋友可以參考下2019-06-06Java簡(jiǎn)單實(shí)現(xiàn)UDP和TCP的示例
下面小編就為大家?guī)?lái)一篇Java簡(jiǎn)單實(shí)現(xiàn)UDP和TCP的示例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Java函數(shù)式編程(一):你好,Lambda表達(dá)式
這篇文章主要介紹了Java函數(shù)式編程(一):你好,Lambda表達(dá)式,本文講解了新老函數(shù)式編程的一些變化,需要的朋友可以參考下2014-09-09SpringBoot+Redis+Lua分布式限流的實(shí)現(xiàn)
本文主要介紹了SpringBoot+Redis+Lua分布式限流的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08關(guān)于通過(guò)Java連接mysql對(duì)反斜杠”\“轉(zhuǎn)義的測(cè)試詳解
這篇文章主要給大家介紹了關(guān)于通過(guò)Java連接mysql對(duì)反斜杠”\“轉(zhuǎn)義的測(cè)試的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家理解反斜杠”\“轉(zhuǎn)義具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06深入理解spring boot異步調(diào)用方式@Async
Spring為任務(wù)調(diào)度與異步方法執(zhí)行提供了注解支持。通過(guò)在方法上設(shè)置@Async注解,可使得方法被異步調(diào)用。下面這篇文章主要給大家介紹了關(guān)于spring boot異步調(diào)用方式@Async的相關(guān)資料,需要的朋友可以參考下。2017-07-07