Java操作mongodb增刪改查的基本操作實(shí)戰(zhàn)指南
一、什么是MongoDB?
MongoDB 是一個(gè)基于分布式文件存儲的數(shù)據(jù)庫。它是一個(gè)介于關(guān)系型數(shù)據(jù)庫和非關(guān)系型數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系型數(shù)據(jù)庫當(dāng)中功能最豐富也是最像關(guān)系型數(shù)據(jù)庫的。
MongDB優(yōu)點(diǎn):1.有很強(qiáng)的的擴(kuò)展性;
2.支持多種編程語言;
3.面向文檔存儲,操作比較簡單;
缺點(diǎn):1.不支持事務(wù); 2.不能進(jìn)行多表聯(lián)查;
二、MongoDB基本操作
1.產(chǎn)看所有庫
show dbs;

2.創(chuàng)建庫
use 庫名;
注:如果庫中沒有數(shù)據(jù),就會是一個(gè)虛擬庫,查看庫的時(shí)候不會顯示該庫
3.查看當(dāng)前庫
db;

4.刪除庫(危險(xiǎn)操作!一般不使用)
db.dropDatabase();
三.java操作MogoDB
在java中使用mongoDB之前,首先需要java連接mongoDB的第三方驅(qū)動包(通過在xml文件中添加依賴)
1.新增
public class AddDemo {
public static void main(String[] args) {
//獲取鏈接
MongoClient mc = new MongoClient("localhost",27017);
//獲取庫對象
MongoDatabase db = mc.getDatabase("student");
//獲取集合對象
MongoCollection<Document> collection = db.getCollection("student");
//新增
Document document = new Document();
document.put("name", "周子舒");
document.put("sex", "男");
document.put("age", 18);
document.put("birthday",new Date());
//添加一條數(shù)據(jù)
collection.insertOne(document);
//添加多條數(shù)據(jù)
Document document1= new Document();
document1.put("name", "鐘無艷");
document1.put("sex", "女");
document1.put("age", 18);
document1.put("birthday",new Date());
Document document2= new Document();
document2.put("name", "貂蟬");
document2.put("sex", "女");
document2.put("age", 1);
document2.put("birthday",new Date());
ArrayList<Document> list = new ArrayList<Document>();
list.add(document1);
list.add(document2);
collection.insertMany(list);
mc.close();
}
}
2.刪除
刪除某條單個(gè)數(shù)據(jù)時(shí),使用 MongoCollection 對象的 deleteOne() 方法,該方法接收一個(gè)數(shù)據(jù)類型為 Bson 的的對象作為過濾器篩選出需要刪除的文檔。注意deleteOne() 方法只會刪除表中滿足刪除條件的第一條數(shù)據(jù)。JDBC驅(qū)動程序提供了 Filters 類來為所有的MongoDB查詢操作提供靜態(tài)方法。每個(gè)方法返回的BSON類型。
public void deleteOneTest(){
public static void main(String[] args) {
//獲取鏈接
MongoClient mc = new MongoClient("localhost",27017);
//獲取庫對象
MongoDatabase db = mc.getDatabase("student");
//獲取集合對象
MongoCollection<Document> collection = db.getCollection("student");
//刪除條件
Bson filter = Filters.eq("age",18);
//刪除與篩選器匹配的單個(gè)文檔
collection.deleteOne(filter);
mc.close();
}
}Filters.eq() //匹配到等于指定值的數(shù)據(jù) Filters.gt() //匹配到大于指定值的數(shù)據(jù) Filters.gte() //匹配到大于等于定值的數(shù)據(jù) Filters.lt() //匹配到小于指定值的數(shù)據(jù)
刪除多條數(shù)據(jù), 使用 MongoCollection 對象的 deleteMany() 方法,該方法會將匹配到的所有數(shù)據(jù)全部刪除。
public class DeleteDemo {
public static void main(String[] args) {
//獲取鏈接
MongoClient mc = new MongoClient("localhost",27017);
//獲取庫對象
MongoDatabase db = mc.getDatabase("student");
//獲取集合對象
MongoCollection<Document> collection = db.getCollection("student");
// Bson exists=Filters.exists("age",false);
Bson gt=Filters.gt("age", 100);
// Bson age=Filters.exists("age");
DeleteResult deleteMany = collection.deleteMany(gt);
System.out.println(deleteMany);
mc.close();
}
}3.修改
修改單個(gè)數(shù)據(jù),使用 MongoCollection 對象的 updateOne() 方法,該方法接收兩個(gè)參數(shù),第一個(gè)數(shù)據(jù)類型為 Bson 的過濾器篩選出需要修改的文檔,第二個(gè)參數(shù)數(shù)據(jù)類型為 Bson 指定如何修改篩選出的文檔。然后修改過濾器篩選出的第一個(gè)文檔。
修改多條數(shù)據(jù),使用 MongoCollection 對象的 updateMany() 方法,也要接受兩個(gè)參數(shù),第一個(gè)是修改條件,第二是修改后的數(shù)據(jù)。
public class UpdateDemo {
public static void main(String[] args) {
//獲取鏈接
MongoClient mc = new MongoClient("localhost",27017);
//獲取庫對象
MongoDatabase db = mc.getDatabase("student");
//獲取集合對象
MongoCollection<Document> collection = db.getCollection("student");
// Bson eq = Filters.eq("name","呂布");
//修改一條數(shù)據(jù)
// UpdateResult updateOne = collection.updateOne(eq, new Document("$set",new Document("age",99)),new UpdateOptions().upsert(true));
// System.out.println(updateOne);
//修改多條數(shù)據(jù)
Bson and=Filters.and(Filters.gt("age",20),Filters.lte("age", 100));
// UpdateResult updateMany = collection.updateMany(eq, new Document("$set",new Document("age",3)));
UpdateResult updateMany = collection.updateMany(and, new Document("$inc",new Document("age",100)));
System.out.println(updateMany);
mc.close();
}
}4.查詢
查詢方式較多,分別為:
1.模糊查詢
Bson b=Filters.regex("name","張");//查出所有姓張的數(shù)據(jù)2.分頁查
FindIterable<Document> b=collection.find().skip(0).limit(3);//跳過第0條數(shù)據(jù),一次看三條數(shù)據(jù)
3.排序查詢
Bson b=new Document("id",-1);//根據(jù)id倒敘排序
FindIterable<Document> d=collection.find().sort(b);public class SelectDemo {
public static void main(String[] args) {
//獲取鏈接
MongoClient mc = new MongoClient("localhost",27017);
//獲取庫對象
MongoDatabase db = mc.getDatabase("student");
//獲取集合對象
MongoCollection<Document> collection = db.getCollection("student");
// 添加條件
Bson eq = Filters.regex("name", "張");
Document document = new Document("birthday",-1);
// .limit(2).skip(2)
FindIterable<Document> find = collection.find(eq).sort(document);
List<Student> slist = new ArrayList<Student>();
MongoCursor<Document> iterator = find.iterator();
while(iterator.hasNext()) {
Student s = new Student();
Document next = iterator.next();
s.setSname(next.getString("name"));
s.setSsex(next.getString("sex"));
s.setSid(next.getInteger("sid"));
// 參數(shù)1 Json 字符串
// 參數(shù)2 需要的對象的類型
// String json = next.toJson();
// System.out.println(json);
// Student s = gson.fromJson(json, Student.class);
slist.add(s);
}
for(Student ss : slist){
System.out.println(ss);
}
mc.close();
}
}總結(jié)
到此這篇關(guān)于Java操作mongodb增刪改查的基本操作的文章就介紹到這了,更多相關(guān)Java操作mongodb增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java調(diào)用dll文件的實(shí)現(xiàn)解析
這篇文章主要介紹了Java調(diào)用dll文件的實(shí)現(xiàn)解析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Post請求參數(shù)是數(shù)組或者List時(shí)的請求處理方式
這篇文章主要介紹了Post請求參數(shù)是數(shù)組或者List時(shí)的請求處理方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
springboot 同時(shí)啟用http/https的配置方法
本文給大家分享springboot 同時(shí)啟用http/https的配置方法,通過修改配置文件、增加java配置的方法來實(shí)現(xiàn)此操作,具體內(nèi)容詳情跟隨小編通過本文學(xué)習(xí)下吧2021-05-05
mybatis-plus之如何實(shí)現(xiàn)in嵌套sql
這篇文章主要介紹了mybatis-plus之如何實(shí)現(xiàn)in嵌套sql問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

