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文件中添加依賴)
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ù)類型為 Bson 的的對(duì)象作為過(guò)濾器篩選出需要?jiǎng)h除的文檔。注意deleteOne() 方法只會(huì)刪除表中滿足刪除條件的第一條數(shù)據(jù)。JDBC驅(qū)動(dòng)程序提供了 Filters 類來(lái)為所有的MongoDB查詢操作提供靜態(tài)方法。每個(gè)方法返回的BSON類型。
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ù)類型為 Bson 的過(guò)濾器篩選出需要修改的文檔,第二個(gè)參數(shù)數(shù)據(jù)類型為 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.查詢
查詢方式較多,分別為:
1.模糊查詢
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.排序查詢
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ì)象的類型
// 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)文章
Java調(diào)用dll文件的實(shí)現(xiàn)解析
這篇文章主要介紹了Java調(diào)用dll文件的實(shí)現(xiàn)解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Java打印星號(hào)圖案和數(shù)字圖案的示例代碼
在 Java 中打印圖案是一項(xiàng)常見(jiàn)的編程任務(wù),尤其在初學(xué)階段,通過(guò)以特定方式排列符號(hào)或數(shù)字,可以形成各種設(shè)計(jì)或形狀,這些圖案不僅有助于解決問(wèn)題,還能培養(yǎng)算法思維能力,本文將討論如何在 Java 中打印圖案,并探索一些最常見(jiàn)的圖案類型,需要的朋友可以參考下2024-11-11
零基礎(chǔ)寫Java知乎爬蟲(chóng)之進(jìn)階篇
前面幾篇文章,我們都是簡(jiǎn)單的實(shí)現(xiàn)了java爬蟲(chóng)抓取內(nèi)容的問(wèn)題,那么如果遇到復(fù)雜情況,我們還能繼續(xù)那么做嗎?答案當(dāng)然是否定的,之前的僅僅是入門篇,都是些基礎(chǔ)知識(shí),給大家練手用的,本文我們就來(lái)點(diǎn)高大上的東西2014-11-11
Post請(qǐng)求參數(shù)是數(shù)組或者List時(shí)的請(qǐng)求處理方式
這篇文章主要介紹了Post請(qǐng)求參數(shù)是數(shù)組或者List時(shí)的請(qǐng)求處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
springboot 同時(shí)啟用http/https的配置方法
本文給大家分享springboot 同時(shí)啟用http/https的配置方法,通過(guò)修改配置文件、增加java配置的方法來(lái)實(shí)現(xiàn)此操作,具體內(nèi)容詳情跟隨小編通過(guò)本文學(xué)習(xí)下吧2021-05-05
mybatis-plus之如何實(shí)現(xiàn)in嵌套sql
這篇文章主要介紹了mybatis-plus之如何實(shí)現(xiàn)in嵌套sql問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

