欧美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ù),無(wú)需包含此頭文件。

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();
//無(wú)需包含上面的頭文件

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ù)庫(kù)相關(guān)API小記

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

MongoDB數(shù)據(jù)庫(kù)的查詢條件或者說(shuō)習(xí)慣對(duì)于使用MySQL數(shù)據(jù)庫(kù)的開(kāi)發(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ù)庫(kù)需要的配置,以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ù)庫(kù)名
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ù)庫(kù)名”);

??? ?@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.修改

修改作用用到的方法是上面說(shuō)的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)文章

  • 解讀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)論