Java操作mongodb增刪改查的基本操作實(shí)戰(zhàn)指南
一、什么是MongoDB?
MongoDB 是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù)。它是一個(gè)介于關(guān)系型數(shù)據(jù)庫(kù)和非關(guān)系型數(shù)據(jù)庫(kù)之間的產(chǎn)品,是非關(guān)系型數(shù)據(jù)庫(kù)當(dāng)中功能最豐富也是最像關(guān)系型數(shù)據(jù)庫(kù)的。
MongDB優(yōu)點(diǎn):1.有很強(qiáng)的的擴(kuò)展性;
2.支持多種編程語(yǔ)言;
3.面向文檔存儲(chǔ),操作比較簡(jiǎn)單;
缺點(diǎn):1.不支持事務(wù); 2.不能進(jìn)行多表聯(lián)查;
二、MongoDB基本操作
1.產(chǎn)看所有庫(kù)
show dbs;

2.創(chuàng)建庫(kù)
use 庫(kù)名;
注:如果庫(kù)中沒(méi)有數(shù)據(jù),就會(huì)是一個(gè)虛擬庫(kù),查看庫(kù)的時(shí)候不會(huì)顯示該庫(kù)
3.查看當(dāng)前庫(kù)
db;

4.刪除庫(kù)(危險(xiǎn)操作!一般不使用)
db.dropDatabase();
三.java操作MogoDB
在java中使用mongoDB之前,首先需要java連接mongoDB的第三方驅(qū)動(dòng)包(通過(guò)在xml文件中添加依賴(lài))
1.新增
public class AddDemo {
public static void main(String[] args) {
//獲取鏈接
MongoClient mc = new MongoClient("localhost",27017);
//獲取庫(kù)對(duì)象
MongoDatabase db = mc.getDatabase("student");
//獲取集合對(duì)象
MongoCollection<Document> collection = db.getCollection("student");
//新增
Document document = new Document();
document.put("name", "周子舒");
document.put("sex", "男");
document.put("age", 18);
document.put("birthday",new Date());
//添加一條數(shù)據(jù)
collection.insertOne(document);
//添加多條數(shù)據(jù)
Document document1= new Document();
document1.put("name", "鐘無(wú)艷");
document1.put("sex", "女");
document1.put("age", 18);
document1.put("birthday",new Date());
Document document2= new Document();
document2.put("name", "貂蟬");
document2.put("sex", "女");
document2.put("age", 1);
document2.put("birthday",new Date());
ArrayList<Document> list = new ArrayList<Document>();
list.add(document1);
list.add(document2);
collection.insertMany(list);
mc.close();
}
}
2.刪除
刪除某條單個(gè)數(shù)據(jù)時(shí),使用 MongoCollection 對(duì)象的 deleteOne() 方法,該方法接收一個(gè)數(shù)據(jù)類(lèi)型為 Bson 的的對(duì)象作為過(guò)濾器篩選出需要?jiǎng)h除的文檔。注意deleteOne() 方法只會(huì)刪除表中滿(mǎn)足刪除條件的第一條數(shù)據(jù)。JDBC驅(qū)動(dòng)程序提供了 Filters 類(lèi)來(lái)為所有的MongoDB查詢(xún)操作提供靜態(tài)方法。每個(gè)方法返回的BSON類(lèi)型。
public void deleteOneTest(){
public static void main(String[] args) {
//獲取鏈接
MongoClient mc = new MongoClient("localhost",27017);
//獲取庫(kù)對(duì)象
MongoDatabase db = mc.getDatabase("student");
//獲取集合對(duì)象
MongoCollection<Document> collection = db.getCollection("student");
//刪除條件
Bson filter = Filters.eq("age",18);
//刪除與篩選器匹配的單個(gè)文檔
collection.deleteOne(filter);
mc.close();
}
}Filters.eq() //匹配到等于指定值的數(shù)據(jù) Filters.gt() //匹配到大于指定值的數(shù)據(jù) Filters.gte() //匹配到大于等于定值的數(shù)據(jù) Filters.lt() //匹配到小于指定值的數(shù)據(jù)
刪除多條數(shù)據(jù), 使用 MongoCollection 對(duì)象的 deleteMany() 方法,該方法會(huì)將匹配到的所有數(shù)據(jù)全部刪除。
public class DeleteDemo {
public static void main(String[] args) {
//獲取鏈接
MongoClient mc = new MongoClient("localhost",27017);
//獲取庫(kù)對(duì)象
MongoDatabase db = mc.getDatabase("student");
//獲取集合對(duì)象
MongoCollection<Document> collection = db.getCollection("student");
// Bson exists=Filters.exists("age",false);
Bson gt=Filters.gt("age", 100);
// Bson age=Filters.exists("age");
DeleteResult deleteMany = collection.deleteMany(gt);
System.out.println(deleteMany);
mc.close();
}
}3.修改
修改單個(gè)數(shù)據(jù),使用 MongoCollection 對(duì)象的 updateOne() 方法,該方法接收兩個(gè)參數(shù),第一個(gè)數(shù)據(jù)類(lèi)型為 Bson 的過(guò)濾器篩選出需要修改的文檔,第二個(gè)參數(shù)數(shù)據(jù)類(lèi)型為 Bson 指定如何修改篩選出的文檔。然后修改過(guò)濾器篩選出的第一個(gè)文檔。
修改多條數(shù)據(jù),使用 MongoCollection 對(duì)象的 updateMany() 方法,也要接受兩個(gè)參數(shù),第一個(gè)是修改條件,第二是修改后的數(shù)據(jù)。
public class UpdateDemo {
public static void main(String[] args) {
//獲取鏈接
MongoClient mc = new MongoClient("localhost",27017);
//獲取庫(kù)對(duì)象
MongoDatabase db = mc.getDatabase("student");
//獲取集合對(duì)象
MongoCollection<Document> collection = db.getCollection("student");
// Bson eq = Filters.eq("name","呂布");
//修改一條數(shù)據(jù)
// UpdateResult updateOne = collection.updateOne(eq, new Document("$set",new Document("age",99)),new UpdateOptions().upsert(true));
// System.out.println(updateOne);
//修改多條數(shù)據(jù)
Bson and=Filters.and(Filters.gt("age",20),Filters.lte("age", 100));
// UpdateResult updateMany = collection.updateMany(eq, new Document("$set",new Document("age",3)));
UpdateResult updateMany = collection.updateMany(and, new Document("$inc",new Document("age",100)));
System.out.println(updateMany);
mc.close();
}
}4.查詢(xún)
查詢(xún)方式較多,分別為:
1.模糊查詢(xún)
Bson b=Filters.regex("name","張");//查出所有姓張的數(shù)據(jù)2.分頁(yè)查
FindIterable<Document> b=collection.find().skip(0).limit(3);//跳過(guò)第0條數(shù)據(jù),一次看三條數(shù)據(jù)
3.排序查詢(xún)
Bson b=new Document("id",-1);//根據(jù)id倒敘排序
FindIterable<Document> d=collection.find().sort(b);public class SelectDemo {
public static void main(String[] args) {
//獲取鏈接
MongoClient mc = new MongoClient("localhost",27017);
//獲取庫(kù)對(duì)象
MongoDatabase db = mc.getDatabase("student");
//獲取集合對(duì)象
MongoCollection<Document> collection = db.getCollection("student");
// 添加條件
Bson eq = Filters.regex("name", "張");
Document document = new Document("birthday",-1);
// .limit(2).skip(2)
FindIterable<Document> find = collection.find(eq).sort(document);
List<Student> slist = new ArrayList<Student>();
MongoCursor<Document> iterator = find.iterator();
while(iterator.hasNext()) {
Student s = new Student();
Document next = iterator.next();
s.setSname(next.getString("name"));
s.setSsex(next.getString("sex"));
s.setSid(next.getInteger("sid"));
// 參數(shù)1 Json 字符串
// 參數(shù)2 需要的對(duì)象的類(lèi)型
// String json = next.toJson();
// System.out.println(json);
// Student s = gson.fromJson(json, Student.class);
slist.add(s);
}
for(Student ss : slist){
System.out.println(ss);
}
mc.close();
}
}總結(jié)
到此這篇關(guān)于Java操作mongodb增刪改查的基本操作的文章就介紹到這了,更多相關(guān)Java操作mongodb增刪改查內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用SpringBoot簡(jiǎn)單了解Druid的監(jiān)控系統(tǒng)的配置方法
這篇文章主要介紹了使用SpringBoot簡(jiǎn)單了解Druid的監(jiān)控系統(tǒng)的配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
解決RestTemplate反序列化嵌套對(duì)象的問(wèn)題
這篇文章主要介紹了解決RestTemplate反序列化嵌套對(duì)象的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
SpringBoot集成JWT實(shí)現(xiàn)token驗(yàn)證的流程
Json web token (JWT), 是為了在網(wǎng)絡(luò)應(yīng)用環(huán)境間傳遞聲明而執(zhí)行的一種基于JSON的開(kāi)放標(biāo)準(zhǔn)((RFC 7519).這篇文章主要介紹了SpringBoot集成JWT實(shí)現(xiàn)token驗(yàn)證,需要的朋友可以參考下2020-01-01
WebUploader+SpringMVC實(shí)現(xiàn)文件上傳功能
WebUploader是由Baidu團(tuán)隊(duì)開(kāi)發(fā)的一個(gè)簡(jiǎn)單的以HTML5為主,F(xiàn)LASH為輔的現(xiàn)代文件上傳組件。這篇文章主要介紹了WebUploader+SpringMVC實(shí)現(xiàn)文件上傳功能,需要的朋友可以參考下2017-06-06
淺析Java語(yǔ)言中狀態(tài)模式的優(yōu)點(diǎn)
狀態(tài)模式允許對(duì)象在內(nèi)部狀態(tài)改變時(shí)改變它的行為,對(duì)象看起來(lái)好像修改了它的類(lèi)。這個(gè)模式將狀態(tài)封裝成獨(dú)立的類(lèi),并將動(dòng)作委托到 代表當(dāng)前狀態(tài)的對(duì)象,我們知道行為會(huì)隨著內(nèi)部狀態(tài)而改變2023-02-02
Springboot實(shí)現(xiàn)自定義錯(cuò)誤頁(yè)面的方法(錯(cuò)誤處理機(jī)制)
這篇文章主要介紹了Springboot實(shí)現(xiàn)自定義錯(cuò)誤頁(yè)面的方法(錯(cuò)誤處理機(jī)制),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
SpringBootAdmin+actuator實(shí)現(xiàn)服務(wù)監(jiān)控
這篇文章主要為大家詳細(xì)介紹了SpringBootAdmin+actuator實(shí)現(xiàn)服務(wù)監(jiān)控,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
mybatis-plus與mybatis共存的實(shí)現(xiàn)
本文主要介紹了mybatis-plus與mybatis共存的實(shí)現(xiàn),文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
spring boot中使用@Async實(shí)現(xiàn)異步調(diào)用任務(wù)
本篇文章主要介紹了spring boot中使用@Async實(shí)現(xiàn)異步調(diào)用任務(wù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02

