2019-04-02 14:37:49 4137瀏覽
今天扣丁學(xué)堂Java培訓(xùn)老師給大家分享一篇關(guān)于java連接mongoDB并進(jìn)行增刪改查操作,結(jié)合實(shí)例形式詳細(xì)分析了java環(huán)境下MongoDB擴(kuò)展包的下載、安裝及操作MongoDB連接、增刪改查等相關(guān)操作技巧。
<dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.0.4</version> </dependency> </dependencies>
//連接到 mongodb 服務(wù) MongoClient mongoClient = new MongoClient("localhost", 27017);
//連接到 mongodb 服務(wù),默認(rèn)端口號(hào)為27017 MongoClient mongoClient = new MongoClient("localhost");
//連接到 mongodb 服務(wù),默認(rèn)連接到localhost服務(wù)器,端口號(hào)為27017 MongoClient mongoClient = new MongoClient();
List<ServerAddress> adds = new ArrayList<>(); //ServerAddress()兩個(gè)參數(shù)分別為 服務(wù)器地址 和 端口 ServerAddress serverAddress = new ServerAddress("localhost", 27017); adds.add(serverAddress); List<MongoCredential> credentials = new ArrayList<>(); //MongoCredential.createScramSha1Credential()三個(gè)參數(shù)分別為 用戶名 數(shù)據(jù)庫(kù)名稱 密碼 MongoCredential mongoCredential = MongoCredential.createScramSha1Credential("username", "databaseName", "password".toCharArray()); credentials.add(mongoCredential); //通過(guò)連接認(rèn)證獲取MongoDB連接 MongoClient mongoClient = new MongoClient(adds, credentials);
//連接到數(shù)據(jù)庫(kù) MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
import com.mongodb.MongoClient; import com.mongodb.client.MongoDatabase; //mongodb 連接數(shù)據(jù)庫(kù)工具類 public class MongoDBUtil { //不通過(guò)認(rèn)證獲取連接數(shù)據(jù)庫(kù)對(duì)象 public static MongoDatabase getConnect(){ //連接到 mongodb 服務(wù) MongoClient mongoClient = new MongoClient("localhost", 27017); //連接到數(shù)據(jù)庫(kù) MongoDatabase mongoDatabase = mongoClient.getDatabase("test"); //返回連接數(shù)據(jù)庫(kù)對(duì)象 return mongoDatabase; } //需要密碼認(rèn)證方式連接 public static MongoDatabase getConnect2(){ List<ServerAddress> adds = new ArrayList<>(); //ServerAddress()兩個(gè)參數(shù)分別為 服務(wù)器地址 和 端口 ServerAddress serverAddress = new ServerAddress("localhost", 27017); adds.add(serverAddress); List<MongoCredential> credentials = new ArrayList<>(); //MongoCredential.createScramSha1Credential()三個(gè)參數(shù)分別為 用戶名 數(shù)據(jù)庫(kù)名稱 密碼 MongoCredential mongoCredential = MongoCredential.createScramSha1Credential("username", "databaseName", "password".toCharArray()); credentials.add(mongoCredential); //通過(guò)連接認(rèn)證獲取MongoDB連接 MongoClient mongoClient = new MongoClient(adds, credentials); //連接到數(shù)據(jù)庫(kù) MongoDatabase mongoDatabase = mongoClient.getDatabase("test"); //返回連接數(shù)據(jù)庫(kù)對(duì)象 return mongoDatabase; } }
//獲取集合 MongoCollection<Document> collection = MongoDBUtil.getConnect().getCollection("user");
//創(chuàng)建文檔 Document document = new Document("name","張三") .append("sex", "男") .append("age", 18);
//插入一個(gè)文檔 @Test public void insertOneTest(){ //獲取數(shù)據(jù)庫(kù)連接對(duì)象 MongoDatabase mongoDatabase = MongoDBUtil.getConnect(); //獲取集合 MongoCollection<Document> collection = mongoDatabase.getCollection("user"); //要插入的數(shù)據(jù) Document document = new Document("name","張三") .append("sex", "男") .append("age", 18); //插入一個(gè)文檔 collection.insertOne(document); }
//插入多個(gè)文檔 @Test public void insertManyTest(){ //獲取數(shù)據(jù)庫(kù)連接對(duì)象 MongoDatabase mongoDatabase = MongoDBUtil.getConnect(); //獲取集合 MongoCollection<Document> collection = mongoDatabase.getCollection("user"); //要插入的數(shù)據(jù) List<Document> list = new ArrayList<>(); for(int i = 1; i <= 3; i++) { Document document = new Document("name", "張三") .append("sex", "男") .append("age", 18); list.add(document); } //插入多個(gè)文檔 collection.insertMany(list); }
//刪除與篩選器匹配的單個(gè)文檔 @Test public void deleteOneTest(){ //獲取數(shù)據(jù)庫(kù)連接對(duì)象 MongoDatabase mongoDatabase = MongoDBUtil.getConnect(); //獲取集合 MongoCollection<Document> collection = mongoDatabase.getCollection("user"); //申明刪除條件 Bson filter = Filters.eq("age",18); //刪除與篩選器匹配的單個(gè)文檔 collection.deleteOne(filter); }
//刪除與篩選器匹配的所有文檔 @Test public void deleteManyTest(){ //獲取數(shù)據(jù)庫(kù)連接對(duì)象 MongoDatabase mongoDatabase = MongoDBUtil.getConnect(); //獲取集合 MongoCollection<Document> collection = mongoDatabase.getCollection("user"); //申明刪除條件 Bson filter = Filters.eq("age",18); //刪除與篩選器匹配的所有文檔 collection.deleteMany(filter); }
//修改單個(gè)文檔 @Test public void updateOneTest(){ //獲取數(shù)據(jù)庫(kù)連接對(duì)象 MongoDatabase mongoDatabase = MongoDBUtil.getConnect(); //獲取集合 MongoCollection<Document> collection = mongoDatabase.getCollection("user"); //修改過(guò)濾器 Bson filter = Filters.eq("name", "張三"); //指定修改的更新文檔 Document document = new Document("$set", new Document("age", 100)); //修改單個(gè)文檔 collection.updateOne(filter, document); }
//修改多個(gè)文檔 @Test public void updateManyTest(){ //獲取數(shù)據(jù)庫(kù)連接對(duì)象 MongoDatabase mongoDatabase = MongoDBUtil.getConnect(); //獲取集合 MongoCollection<Document> collection = mongoDatabase.getCollection("user"); //修改過(guò)濾器 Bson filter = Filters.eq("name", "張三"); //指定修改的更新文檔 Document document = new Document("$set", new Document("age", 100)); //修改多個(gè)文檔 collection.updateMany(filter, document); }
//查找集合中的所有文檔 @Test public void findTest(){ //獲取數(shù)據(jù)庫(kù)連接對(duì)象 MongoDatabase mongoDatabase = MongoDBUtil.getConnect(); //獲取集合 MongoCollection<Document> collection = mongoDatabase.getCollection("user"); //查找集合中的所有文檔 FindIterable findIterable = collection.find(); MongoCursor cursor = findIterable.iterator(); while (cursor.hasNext()) { System.out.println(cursor.next()); } }
//指定查詢過(guò)濾器查詢 @Test public void FilterfindTest(){ //獲取數(shù)據(jù)庫(kù)連接對(duì)象 MongoDatabase mongoDatabase = MongoDBUtil.getConnect(); //獲取集合 MongoCollection<Document> collection = mongoDatabase.getCollection("user"); //指定查詢過(guò)濾器 Bson filter = Filters.eq("name", "張三"); //指定查詢過(guò)濾器查詢 FindIterable findIterable = collection.find(filter); MongoCursor cursor = findIterable.iterator(); while (cursor.hasNext()) { System.out.println(cursor.next()); } }
//取出查詢到的第一個(gè)文檔 @Test public void findTest(){ //獲取數(shù)據(jù)庫(kù)連接對(duì)象 MongoDatabase mongoDatabase = MongoDBUtil.getConnect(); //獲取集合 MongoCollection<Document> collection = mongoDatabase.getCollection("user"); //查找集合中的所有文檔 FindIterable findIterable = collection.find(); //取出查詢到的第一個(gè)文檔 Document document = (Document) findIterable.first(); //打印輸出 System.out.println(document); }以上就是關(guān)于扣丁學(xué)堂java培訓(xùn)之連接mongoDB并進(jìn)行增刪改查操作實(shí)例詳解,想要學(xué)好Java開(kāi)發(fā)小編給大家推薦口碑良好的扣丁學(xué)堂,扣丁學(xué)堂有專業(yè)老師制定的Java學(xué)習(xí)路線圖輔助學(xué)員學(xué)習(xí),此外還有與時(shí)俱進(jìn)的Java視頻教程供大家學(xué)習(xí),想要學(xué)好Java開(kāi)發(fā)技術(shù)的小伙伴快快行動(dòng)吧??鄱W(xué)堂Java技術(shù)交流群:670348138。
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入HTML5前端開(kāi)發(fā)VIP免費(fèi)公開(kāi)課】
查看更多關(guān)于“Java開(kāi)發(fā)資訊”的相關(guān)文章>>