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

Java如何操作MongoDB常用API文檔

 更新時(shí)間:2022年07月08日 10:05:47   作者:john__rambo  
這篇文章主要介紹了Java如何操作MongoDB常用API文檔,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Java操作MongoDB常用API文檔

1.查詢指定字段

collection.find().projection(fields(include("username","pwd"),excludeId()));//返回username與pwd字段且不返回_id字段
Document doc = new Document().append("_id", 0).append("username",1).append("pwd",1);//指定查詢字段,0為不包含此字段,1為包含此字段
FindIterable<Document> findIterable = collection.find().projection(doc);

兩種方法查詢結(jié)果相同,區(qū)別是第一種方法使用了include等函數(shù),需要包含頭文件 import static com.mongodb.client.model.Projections.*;

第二種方法使用Document代替了include等函數(shù),無需包含此頭文件。

2.按條件查詢

Document myDoc = collection.find(and(eq("username","liuchao"),eq("pwd","12345"))).first();
//此方法需包含頭文件import static com.mongodb.client.model.Filters.*;
Document myDoc = collection.find(new Document("username", "liuchao").append("pwd", "12345")).first();
//無需包含上面的頭文件

3.對(duì)查詢結(jié)果排序

FindIterable<Document> iterable = collection.find().sort(ascending("title"));//按title升序排列
FindIterable<Document> iterable = collection.find().sort(ascending("title","words"));//按title和words升序排列
FindIterable<Document> iterable = collection.find().sort(descending("title"));//按title降序排列
FindIterable<Document> iterable = collection.find().sort(new Document("time",-1));//按time降序排列

4.獲取滿足條件的前n條數(shù)據(jù)

MongoCursor<Document> cursor = collection.find(new Document("username","liuchao")).sort(new Document("time",-1)).limit(n).iterator();
//對(duì)滿足條件username=“l(fā)iuchao”的結(jié)果進(jìn)行降序排列,并獲取前n條數(shù)據(jù)。(n=0獲取全部)

Java使用MongoDB數(shù)據(jù)庫相關(guān)API小記

MongDB數(shù)據(jù)庫使用

MongoDB數(shù)據(jù)庫的查詢條件或者說習(xí)慣對(duì)于使用MySQL數(shù)據(jù)庫的開發(fā)人員不太友好,這里總結(jié)一些API的使用心得。

首先介紹我的使用環(huán)境

1.坐標(biāo)的使用:導(dǎo)入springboot的相關(guān)坐標(biāo)

?? ?<!--MongoDB相關(guān)依賴-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-data-mongodb</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.mongodb</groupId>
? ? ? ? ? ? <artifactId>mongodb-driver-sync</artifactId>
? ? ? ? </dependency>

2.驅(qū)動(dòng)坐標(biāo)及其版本號(hào):spring-boot-starter-data-mongodb 中查到的MongoDB版本號(hào)為3.8.2版(不需要寫上去,這里只是明確引入坐標(biāo)的版本)

? ? <dependency>
? ? ? <groupId>org.mongodb</groupId>
? ? ? <artifactId>mongodb-driver</artifactId>
? ? ? <version>3.8.2</version>
? ? ? <scope>compile</scope>
? ? </dependency>

3.MongoDB數(shù)據(jù)庫需要的配置,以springboot框架為例,配置文件如下:

文件名:application.properties

#springboot MongoDB配置
spring.data.mongodb.username=用戶名
spring.data.mongodb.password=密碼
spring.data.mongodb.authentication-database=一般是admin
spring.data.mongodb.database=數(shù)據(jù)庫名
spring.data.mongodb.port=端口號(hào),默認(rèn)27017
spring.data.mongodb.host=主機(jī)ip

4.相關(guān)API(以下API都是調(diào)用方法,而非底層源碼)

  • a.保存(保存分為save方法和insert方法)

save有添加的作用,也有更新的作用; 取決于存入的id是否在文檔中已經(jīng)存在

save(T t),save(T t,String “數(shù)據(jù)庫名”);

??? ?@Autowired
? ? private MongoTemplate mongoTemplate;
?? ? /**
? ? ?* save有添加的作用,也有更新的作用; 取決于存入的id是否在文檔中已經(jīng)存在
? ? ?* @param student 學(xué)生對(duì)象
? ? ?*/
?? ? public void saveStudent(Student student)
? ? {
? ? ? ? //返回的結(jié)果是保存的對(duì)象本身, 所以一般不寫
? ? ? ? Student result = mongoTemplate.save(student);
? ? }
  • b.刪除
?? ?/**
? ? ?* 根據(jù)ObjectId刪除記錄
? ? ?*
? ? ?* @param id ObjectID
? ? ?*/
? ? public void delStudentByObjectId(ObjectId id)
? ? {
? ? ? ? //構(gòu)造查詢條件
? ? ? ? Query query = Query.query(Criteria.where("id").is(id));
? ? ? ? //刪除操作(常用API)
? ? ? ? mongoTemplate.remove(query, Student.class);
? ? }
  • c.修改

修改作用用到的方法是上面說的save方法

  • d.查詢

根據(jù)ObjectId查詢單個(gè)

?? ? /**
? ? ?* 根據(jù)ObjectId查找單條記錄
? ? ?* @param id ObjectId
? ? ?* @return 結(jié)果
? ? ?*/
? ? public Student findStudent(ObjectId id)
? ? {
? ? ? ? Query query = Query.query(Criteria.where("id").is(id));
? ? ? ? //常用API
? ? ? ? Student student = mongoTemplate.findOne(query, Student.class);
? ? ? ? return student;
? ? }

查詢所有

??? ?public List<Student> findList()
? ? {
? ? ? ? //第一個(gè)參數(shù)可以理解為查出的結(jié)果應(yīng)該封裝到怎樣的對(duì)象中,第二個(gè)參數(shù)可以理解為表名(也可以不寫)
? ? ? ? return mongoTemplate.findAll(Student.class, "tb_student");
? ? }

根據(jù)條件查找

比如查找ObjectId不是1的所有數(shù)據(jù)

?? ?public List<Student> findByCondition()
? ? {
? ? ?? ?//構(gòu)造查詢條件
? ? ? ? Query query=Query.query(Criteria.where("id").ne(1));
? ? ? ? //執(zhí)行查找操作,最后一個(gè)參數(shù)可以省略(在實(shí)體類上已經(jīng)加上了注解,指定了對(duì)應(yīng)的表名也就是document的名字)
? ? ? ? List<Student> list = mongoTemplate.find(query, Student.class, "tb_student");
? ? ? ? return list;
? ? }

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java面試崗常見問題之ArrayList和LinkedList的區(qū)別

    Java面試崗常見問題之ArrayList和LinkedList的區(qū)別

    ArrayList和LinkedList作為我們Java中最常使用的集合類,很多人在被問到他們的區(qū)別時(shí),憋了半天僅僅冒出一句:一個(gè)是數(shù)組一個(gè)是鏈表。這樣回答簡直讓面試官吐血。為了讓兄弟們打好基礎(chǔ),我們通過實(shí)際的使用測試,好好說一下ArrayList和LinkedList的區(qū)別這道經(jīng)典的面試題
    2022-01-01
  • SpringBoot攔截器實(shí)現(xiàn)對(duì)404和500等錯(cuò)誤的攔截

    SpringBoot攔截器實(shí)現(xiàn)對(duì)404和500等錯(cuò)誤的攔截

    本篇文章主要介紹了SpringBoot攔截器實(shí)現(xiàn)對(duì)404和500等錯(cuò)誤的攔截,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • Java文本文件操作方法實(shí)例詳解

    Java文本文件操作方法實(shí)例詳解

    這篇文章主要介紹了Java文本文件操作方法,以實(shí)例形式較為詳細(xì)的分析了java操作文本文件的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • java中List刪除時(shí)需要的注意事項(xiàng)

    java中List刪除時(shí)需要的注意事項(xiàng)

    最近在利用java中的LIST在刪除時(shí)發(fā)現(xiàn)了一個(gè)錯(cuò)我,通過查找相關(guān)的資料終于解決了,覺著有必要分享處理給同樣遇到這個(gè)問題的朋友參考,下面這篇文章主要介紹了java中List刪除時(shí)需要的注意事項(xiàng),需要的朋友可以一起來看看吧。
    2017-01-01
  • Idea中添加Maven項(xiàng)目支持scala的詳細(xì)步驟

    Idea中添加Maven項(xiàng)目支持scala的詳細(xì)步驟

    這篇文章主要介紹了Idea中添加Maven項(xiàng)目支持scala,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • java如何獲得redis所有的key-value

    java如何獲得redis所有的key-value

    這篇文章主要介紹了java如何獲得redis所有的key-value,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java職責(zé)鏈模式的深入了解

    Java職責(zé)鏈模式的深入了解

    這篇文章主要為大家介紹了Java職責(zé)鏈模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • Spring Data JPA中的Specification動(dòng)態(tài)查詢詳解

    Spring Data JPA中的Specification動(dòng)態(tài)查詢詳解

    Specification是一個(gè)設(shè)計(jì)模式,用于企業(yè)級(jí)應(yīng)用開發(fā)中,其主要目的是將業(yè)務(wù)規(guī)則從業(yè)務(wù)邏輯中分離出來,在數(shù)據(jù)查詢方面,Specification可以定義復(fù)雜的查詢,使其更易于重用和測試,這篇文章主要介紹了Spring Data JPA中的Specification動(dòng)態(tài)查詢詳解,需要的朋友可以參考下
    2023-07-07
  • 解讀Integer類的parseInt和valueOf的區(qū)別

    解讀Integer類的parseInt和valueOf的區(qū)別

    這篇文章主要介紹了解讀Integer類的parseInt和valueOf的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • springboot實(shí)現(xiàn)防重復(fù)提交和防重復(fù)點(diǎn)擊的示例

    springboot實(shí)現(xiàn)防重復(fù)提交和防重復(fù)點(diǎn)擊的示例

    這篇文章主要介紹了springboot實(shí)現(xiàn)防重復(fù)提交和防重復(fù)點(diǎn)擊的示例,幫助大家更好的理解和學(xué)習(xí)springboot框架,感興趣的朋友可以了解下
    2020-09-09

最新評(píng)論