最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java連接MongoDB的常用方法詳解

 更新時(shí)間:2022年07月18日 16:42:59   作者:崇令  
這篇文章主要為大家詳細(xì)介紹一下Java語言連接MongoDB的常用方法以及實(shí)現(xiàn)增刪改查功能的示例代碼,感興趣的小伙伴可以跟隨小編一起了解一下

一、Java鏈接MongoDB

1. 導(dǎo)入Mongo驅(qū)動(dòng)包

2. 獲取Mongo鏈接對象

MongoClient mc = new MongoClient("localhost",27017);

3. 關(guān)閉鏈接

mc.close();

二、查看庫,查看集合

1. 獲取庫對象

MongoDatabase db = mc.getDatabase("myschool");

2. 獲取庫中表的集合

MongoIterable<String> listCollectionNames = db.listCollectionNames();
        
MongoCursor<String> iterator = listCollectionNames.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }

三、Java對MongoDB增刪改查

1. 添加數(shù)據(jù)

a. 添加一條數(shù)據(jù)

//創(chuàng)建對象
Student s = new Student();
s.setSid(1);
s.setSname("王俊凱");
s.setBirthday(new Date());
s.setSsex("男");
s.setClassid(2);
 
//將數(shù)據(jù)轉(zhuǎn)換為json格式
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
String json = gson.toJson(s);
 
//獲取集合對象
MongoCollection<Document> collection = db.getCollection("student");
 
//添加一條數(shù)據(jù),將json格式轉(zhuǎn)換為document對象
collection.insertOne(Document.parse(json));

b. 添加多條數(shù)據(jù)

//存入數(shù)據(jù)
List<Document> dlist=new ArrayList<Document>();
 
for(int i=0; i<3; i++){
    Student s = new Student();
    s.setSid(Integer.toString(i+1));
    s.setSname("王源");
    s.setBirthday(new Date());
    s.setSsex("男");
    s.setClassid(1);
    //將數(shù)據(jù)轉(zhuǎn)換為json格式
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
    String json = gson.toJson(s);
    dlist.add(Document.parse(json));
}
 
//獲取集合對象
MongoCollection<Document> collection = db.getCollection("student");
 
//添加多條數(shù)據(jù)
collection.insertMany(dlist);

2. 刪除數(shù)據(jù)

a. 刪除一條數(shù)據(jù)

//獲取集合對象
MongoCollection<Document> collection = db.getCollection("student");
 
Student s = new Student();
s.setSid(1);
 
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
Bson bson = Document.parse(gson.toJson(s));
 
DeleteResult deleteOne = collection.deleteOne(bson);

b. 刪除多條數(shù)據(jù)

//獲取集合對象
MongoCollection<Document> collection = db.getCollection("student");
 
Student s = new Student();
s.setSname("王源");
 
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
Bson bson = Document.parse(gson.toJson(s));
 
DeleteResult deleteMany = collection.deleteMany(bson);

3. 修改數(shù)據(jù)

a. 修改一條數(shù)據(jù)

MongoCollection<Document> collection = db.getCollection("student");
 
//一個(gè)條件對象
Bson eq = Filters.eq("sname","易烊千璽");
 
//要修改的數(shù)據(jù)
Document doc = new Document();
doc.put("$set", new Document("age",22));
UpdateResult  updateone = collection.updateOne(eq, doc);
System.out.println(updateone);

b. 修改多條數(shù)據(jù)

MongoCollection<Document> collection = db.getCollection("student");
 
//多條件
Bson bson = Filters.and(Filters.gte("age", 20),Filters.lte("age", 40));
        
//要修改的數(shù)據(jù)
Document doc = new Document();        
doc.put("$set", new Document("sex","男"));
UpdateResult updateMany = collection.updateMany(bson, doc);
System.out.println(updateMany);

4. 查詢數(shù)據(jù)

a. 全查

MongoCollection<Document> collection = db.getCollection("student");
 
FindIterable<Document> findAll = collection.find();
 
MongoCursor<Document> iterator = findAll.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

b. 帶條件查詢

MongoCollection<Document> collection = db.getCollection("student");
 
//一個(gè)條件對象
Bson eq = Filters.eq("sname","易烊千璽");
 
FindIterable<Document> findOne = collection.find(eq);
 
MongoCursor<Document> iterator = findOne.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

c. 模糊查詢

MongoCollection<Document> collection = db.getCollection("student");
 
//使用正則表達(dá)式進(jìn)行模糊查找
Bson eq = Filters.regex("sname","易");
 
FindIterable<Document> find = collection.find(eq);
 
MongoCursor<Document> iterator = find.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

d. 分頁查詢

MongoCollection<Document> collection = db.getCollection("student");
 
//分頁查詢
FindIterable<Document> find = collection.find().skip(2).limit(3);
 
MongoCursor<Document> iterator = find.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

e. 排序查詢

MongoCollection<Document> collection = db.getCollection("student");
 
//排序查詢  1升序   -1降序
Bson bson = new Document("sid",1);
FindIterable<Document> find = collection.find().sort(bson);
 
MongoCursor<Document> iterator = find.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}

到此這篇關(guān)于Java連接MongoDB的常用方法詳解的文章就介紹到這了,更多相關(guān)Java連接MongoDB內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis中動(dòng)態(tài)SQL,if,where,foreach的使用教程詳解

    Mybatis中動(dòng)態(tài)SQL,if,where,foreach的使用教程詳解

    MyBatis的動(dòng)態(tài)SQL是基于OGNL表達(dá)式的,它可以幫助我們方便的在SQL語句中實(shí)現(xiàn)某些邏輯。這篇文章主要介紹了Mybatis中動(dòng)態(tài)SQL,if,where,foreach的使用教程,需要的朋友可以參考下
    2017-11-11
  • Java實(shí)現(xiàn)PDF轉(zhuǎn)圖片的三種方法

    Java實(shí)現(xiàn)PDF轉(zhuǎn)圖片的三種方法

    有些時(shí)候我們需要在項(xiàng)目中展示PDF,所以我們可以將PDF轉(zhuǎn)為圖片,然后已圖片的方式展示,效果很好,Java使用各種技術(shù)將pdf轉(zhuǎn)換成圖片格式,并且內(nèi)容不失幀,本文給大家介紹了三種方法實(shí)現(xiàn)PDF轉(zhuǎn)圖片的案例,需要的朋友可以參考下
    2023-10-10
  • Java使用反射獲取字段屬性

    Java使用反射獲取字段屬性

    這篇文章主要為大家詳細(xì)介紹了Java如何利用反射實(shí)現(xiàn)獲取字段屬性值,文中的示例代碼講解詳細(xì),具有很好的參考價(jià)值,希望對大家有所幫助
    2023-06-06
  • java Stream操作轉(zhuǎn)換方法

    java Stream操作轉(zhuǎn)換方法

    文章總結(jié)了Java 8中流(Stream) API的多種常用方法,包括創(chuàng)建流、過濾、遍歷、分組、排序、去重、查找、匹配、轉(zhuǎn)換、歸約、打印日志、最大最小值、統(tǒng)計(jì)、連接、函數(shù)式接口等,展示了流API在處理集合數(shù)據(jù)時(shí)的強(qiáng)大和靈活性,感興趣的朋友跟隨小編一起看看吧
    2025-01-01
  • Spring中的@Aspect注解使用詳解

    Spring中的@Aspect注解使用詳解

    這篇文章主要介紹了Spring中的@Aspect注解使用詳解,利用AOP可以對業(yè)務(wù)邏輯的各個(gè)部分進(jìn)行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時(shí)提高了開發(fā)的效率,需要的朋友可以參考下
    2024-01-01
  • Java并發(fā)編程示例(三):線程中斷

    Java并發(fā)編程示例(三):線程中斷

    這篇文章主要介紹了Java并發(fā)編程示例(三):線程中斷,在本節(jié),我們所開發(fā)的示例程序?qū)?huì)創(chuàng)建一個(gè)線程,五秒鐘后,利用中斷機(jī)制強(qiáng)制中止這個(gè)線程,需要的朋友可以參考下
    2014-12-12
  • Java利用隨機(jī)分錢模擬財(cái)富變化

    Java利用隨機(jī)分錢模擬財(cái)富變化

    這篇文章主要為大家詳細(xì)介紹了Java如何利用隨機(jī)分錢思想模擬財(cái)富的變化,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-12-12
  • Java中IO流解析及代碼實(shí)例

    Java中IO流解析及代碼實(shí)例

    下面小編就為大家?guī)硪黄P(guān)于Java中的IO流總結(jié)(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2021-07-07
  • Java中淺拷貝與深拷貝實(shí)例解析

    Java中淺拷貝與深拷貝實(shí)例解析

    這篇文章主要給大家介紹了關(guān)于Java中淺拷貝與深拷貝的相關(guān)資料,拷貝對象是java中經(jīng)常會(huì)遇到的問題,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • SpringBoot的jar包如何啟動(dòng)的實(shí)現(xiàn)

    SpringBoot的jar包如何啟動(dòng)的實(shí)現(xiàn)

    本文主要介紹了SpringBoot的jar包如何啟動(dòng)的實(shí)現(xiàn),文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評論

怀化市| 米脂县| 莫力| 清水河县| 资溪县| 陈巴尔虎旗| 任丘市| 三台县| 体育| 丰城市| 雅安市| 丹棱县| 白山市| 阳城县| 浦东新区| 封开县| 华蓥市| 郎溪县| 屏边| 二手房| 上饶市| 玛沁县| 平顺县| 紫阳县| 佛学| 隆德县| 吉水县| 天津市| 延寿县| 西乡县| 兴业县| 包头市| 梁山县| 两当县| 建湖县| 内江市| 开远市| 高尔夫| 丘北县| 曲周县| 青铜峡市|