Java連接MongoDB的常用方法詳解

一、Java鏈接MongoDB

1. 導入Mongo驅動包

2. 獲取Mongo鏈接對象

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

3. 關閉鏈接

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. 添加數據

a. 添加一條數據

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

b. 添加多條數據

//存入數據
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);
    //將數據轉換為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");
 
//添加多條數據
collection.insertMany(dlist);

2. 刪除數據

a. 刪除一條數據

//獲取集合對象
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. 刪除多條數據

//獲取集合對象
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. 修改數據

a. 修改一條數據

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

b. 修改多條數據

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

4. 查詢數據

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");
 
//一個條件對象
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");
 
//使用正則表達式進行模糊查找
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()); 
}

到此這篇關於Java連接MongoDB的常用方法詳解的文章就介紹到這瞭,更多相關Java連接MongoDB內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: