Java如何操作MongoDB常用API文檔
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.對查詢結(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();
//對滿足條件username=“l(fā)iuchao”的結(jié)果進行降序排列,并獲取前n條數(shù)據(jù)。(n=0獲取全部)Java使用MongoDB數(shù)據(jù)庫相關(guān)API小記
MongDB數(shù)據(jù)庫使用
MongoDB數(shù)據(jù)庫的查詢條件或者說習慣對于使用MySQL數(shù)據(jù)庫的開發(fā)人員不太友好,這里總結(jié)一些API的使用心得。
首先介紹我的使用環(huán)境
1.坐標的使用:導入springboot的相關(guān)坐標
?? ?<!--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ū)動坐標及其版本號:spring-boot-starter-data-mongodb 中查到的MongoDB版本號為3.8.2版(不需要寫上去,這里只是明確引入坐標的版本)
? ? <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=端口號,默認27017 spring.data.mongodb.host=主機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 學生對象
? ? ?*/
?? ? public void saveStudent(Student student)
? ? {
? ? ? ? //返回的結(jié)果是保存的對象本身, 所以一般不寫
? ? ? ? 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查詢單個
?? ? /**
? ? ?* 根據(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()
? ? {
? ? ? ? //第一個參數(shù)可以理解為查出的結(jié)果應(yīng)該封裝到怎樣的對象中,第二個參數(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í)行查找操作,最后一個參數(shù)可以省略(在實體類上已經(jīng)加上了注解,指定了對應(yīng)的表名也就是document的名字)
? ? ? ? List<Student> list = mongoTemplate.find(query, Student.class, "tb_student");
? ? ? ? return list;
? ? }以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java面試崗常見問題之ArrayList和LinkedList的區(qū)別
ArrayList和LinkedList作為我們Java中最常使用的集合類,很多人在被問到他們的區(qū)別時,憋了半天僅僅冒出一句:一個是數(shù)組一個是鏈表。這樣回答簡直讓面試官吐血。為了讓兄弟們打好基礎(chǔ),我們通過實際的使用測試,好好說一下ArrayList和LinkedList的區(qū)別這道經(jīng)典的面試題2022-01-01
SpringBoot攔截器實現(xiàn)對404和500等錯誤的攔截
本篇文章主要介紹了SpringBoot攔截器實現(xiàn)對404和500等錯誤的攔截,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04
Spring Data JPA中的Specification動態(tài)查詢詳解
Specification是一個設(shè)計模式,用于企業(yè)級應(yīng)用開發(fā)中,其主要目的是將業(yè)務(wù)規(guī)則從業(yè)務(wù)邏輯中分離出來,在數(shù)據(jù)查詢方面,Specification可以定義復雜的查詢,使其更易于重用和測試,這篇文章主要介紹了Spring Data JPA中的Specification動態(tài)查詢詳解,需要的朋友可以參考下2023-07-07
解讀Integer類的parseInt和valueOf的區(qū)別
這篇文章主要介紹了解讀Integer類的parseInt和valueOf的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
springboot實現(xiàn)防重復提交和防重復點擊的示例
這篇文章主要介紹了springboot實現(xiàn)防重復提交和防重復點擊的示例,幫助大家更好的理解和學習springboot框架,感興趣的朋友可以了解下2020-09-09

