欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java操作mongodb增刪改查的基本操作實戰(zhàn)指南

 更新時間:2023年05月25日 12:03:55   作者:acccco  
MongoDB是一個基于分布式文件存儲的數(shù)據(jù)庫,由c++語言編寫,旨在為WEB應(yīng)用提供可擴展的高性能數(shù)據(jù)存儲解決方案,下面這篇文章主要給大家介紹了關(guān)于Java操作mongodb增刪改查的基本操作實戰(zhàn)指南,需要的朋友可以參考下

一、什么是MongoDB?

MongoDB 是一個基于分布式文件存儲的數(shù)據(jù)庫。它是一個介于關(guān)系型數(shù)據(jù)庫和非關(guān)系型數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系型數(shù)據(jù)庫當中功能最豐富也是最像關(guān)系型數(shù)據(jù)庫的。

MongDB優(yōu)點:1.有很強的的擴展性;

                         2.支持多種編程語言;

                         3.面向文檔存儲,操作比較簡單;

缺點:1.不支持事務(wù);    2.不能進行多表聯(lián)查;

二、MongoDB基本操作

1.產(chǎn)看所有庫

show dbs;

2.創(chuàng)建庫

use 庫名;

注:如果庫中沒有數(shù)據(jù),就會是一個虛擬庫,查看庫的時候不會顯示該庫

3.查看當前庫

db;

4.刪除庫(危險操作!一般不使用)

db.dropDatabase();

三.java操作MogoDB

在java中使用mongoDB之前,首先需要java連接mongoDB的第三方驅(qū)動包(通過在xml文件中添加依賴)

1.新增

public class AddDemo {
	public static void main(String[] args) {
		//獲取鏈接
		MongoClient mc = new MongoClient("localhost",27017);
		//獲取庫對象
		MongoDatabase db = mc.getDatabase("student");
		//獲取集合對象
		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", "鐘無艷");
		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.刪除

刪除某條單個數(shù)據(jù)時,使用 MongoCollection 對象的 deleteOne() 方法,該方法接收一個數(shù)據(jù)類型為 Bson 的的對象作為過濾器篩選出需要刪除的文檔。注意deleteOne() 方法只會刪除表中滿足刪除條件的第一條數(shù)據(jù)。JDBC驅(qū)動程序提供了 Filters 類來為所有的MongoDB查詢操作提供靜態(tài)方法。每個方法返回的BSON類型。

public void deleteOneTest(){
    public static void main(String[] args) {
        //獲取鏈接
		MongoClient mc = new MongoClient("localhost",27017);
		//獲取庫對象
		MongoDatabase db = mc.getDatabase("student");
		//獲取集合對象
		MongoCollection<Document> collection = db.getCollection("student");
        //刪除條件
        Bson filter = Filters.eq("age",18);
        //刪除與篩選器匹配的單個文檔
        collection.deleteOne(filter);
        mc.close();
    }
}
 Filters.eq()   //匹配到等于指定值的數(shù)據(jù)
 Filters.gt()   //匹配到大于指定值的數(shù)據(jù)
 Filters.gte()   //匹配到大于等于定值的數(shù)據(jù)
 Filters.lt()   //匹配到小于指定值的數(shù)據(jù)

刪除多條數(shù)據(jù), 使用 MongoCollection 對象的 deleteMany() 方法,該方法會將匹配到的所有數(shù)據(jù)全部刪除。

public class DeleteDemo {
	public static void main(String[] args) {
		//獲取鏈接
		MongoClient mc = new MongoClient("localhost",27017);
		//獲取庫對象
		MongoDatabase db = mc.getDatabase("student");
		//獲取集合對象
		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.修改

修改單個數(shù)據(jù),使用 MongoCollection 對象的 updateOne() 方法,該方法接收兩個參數(shù),第一個數(shù)據(jù)類型為 Bson 的過濾器篩選出需要修改的文檔,第二個參數(shù)數(shù)據(jù)類型為 Bson 指定如何修改篩選出的文檔。然后修改過濾器篩選出的第一個文檔。

修改多條數(shù)據(jù),使用 MongoCollection 對象的 updateMany() 方法,也要接受兩個參數(shù),第一個是修改條件,第二是修改后的數(shù)據(jù)。

public class UpdateDemo {
	public static void main(String[] args) {
		//獲取鏈接
				MongoClient mc = new MongoClient("localhost",27017);
				//獲取庫對象
				MongoDatabase db = mc.getDatabase("student");
				//獲取集合對象
				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.分頁查

FindIterable<Document> b=collection.find().skip(0).limit(3);//跳過第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);
		//獲取庫對象
		MongoDatabase db = mc.getDatabase("student");
		//獲取集合對象
		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 需要的對象的類型
//					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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用SpringBoot簡單了解Druid的監(jiān)控系統(tǒng)的配置方法

    使用SpringBoot簡單了解Druid的監(jiān)控系統(tǒng)的配置方法

    這篇文章主要介紹了使用SpringBoot簡單了解Druid的監(jiān)控系統(tǒng)的配置,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • 解決RestTemplate反序列化嵌套對象的問題

    解決RestTemplate反序列化嵌套對象的問題

    這篇文章主要介紹了解決RestTemplate反序列化嵌套對象的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • SpringBoot集成JWT實現(xiàn)token驗證的流程

    SpringBoot集成JWT實現(xiàn)token驗證的流程

    Json web token (JWT), 是為了在網(wǎng)絡(luò)應(yīng)用環(huán)境間傳遞聲明而執(zhí)行的一種基于JSON的開放標準((RFC 7519).這篇文章主要介紹了SpringBoot集成JWT實現(xiàn)token驗證,需要的朋友可以參考下
    2020-01-01
  • WebUploader+SpringMVC實現(xiàn)文件上傳功能

    WebUploader+SpringMVC實現(xiàn)文件上傳功能

    WebUploader是由Baidu團隊開發(fā)的一個簡單的以HTML5為主,F(xiàn)LASH為輔的現(xiàn)代文件上傳組件。這篇文章主要介紹了WebUploader+SpringMVC實現(xiàn)文件上傳功能,需要的朋友可以參考下
    2017-06-06
  • 淺析Java語言中狀態(tài)模式的優(yōu)點

    淺析Java語言中狀態(tài)模式的優(yōu)點

    狀態(tài)模式允許對象在內(nèi)部狀態(tài)改變時改變它的行為,對象看起來好像修改了它的類。這個模式將狀態(tài)封裝成獨立的類,并將動作委托到 代表當前狀態(tài)的對象,我們知道行為會隨著內(nèi)部狀態(tài)而改變
    2023-02-02
  • java實現(xiàn)基因序列比較的示例代碼

    java實現(xiàn)基因序列比較的示例代碼

    這篇文章主要介紹了java實現(xiàn)基因序列比較的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • Springboot實現(xiàn)自定義錯誤頁面的方法(錯誤處理機制)

    Springboot實現(xiàn)自定義錯誤頁面的方法(錯誤處理機制)

    這篇文章主要介紹了Springboot實現(xiàn)自定義錯誤頁面的方法(錯誤處理機制),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • SpringBootAdmin+actuator實現(xiàn)服務(wù)監(jiān)控

    SpringBootAdmin+actuator實現(xiàn)服務(wù)監(jiān)控

    這篇文章主要為大家詳細介紹了SpringBootAdmin+actuator實現(xiàn)服務(wù)監(jiān)控,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • mybatis-plus與mybatis共存的實現(xiàn)

    mybatis-plus與mybatis共存的實現(xiàn)

    本文主要介紹了mybatis-plus與mybatis共存的實現(xiàn),文中根據(jù)實例編碼詳細介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • spring boot中使用@Async實現(xiàn)異步調(diào)用任務(wù)

    spring boot中使用@Async實現(xiàn)異步調(diào)用任務(wù)

    本篇文章主要介紹了spring boot中使用@Async實現(xiàn)異步調(diào)用任務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02

最新評論