麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 數(shù)據(jù)庫 > MongoDB > 正文

MongoDB快速入門筆記(八)之MongoDB的java驅(qū)動操作代碼講解

2020-03-14 13:12:04
字體:
供稿:網(wǎng)友

MongoDB的Java驅(qū)動是線程安全的,對于一般的應(yīng)用,只要一個Mongo實例即可,Mongo有個內(nèi)置的連接池(池大小默認(rèn)為10個)。

下面代碼給大家介紹MongoDB的java驅(qū)動操作,具體代碼如下所示:

import java.util.ArrayList;import java.util.List;import java.util.regex.Pattern;import org.bson.Document;import com.mongodb.MongoClient;import com.mongodb.MongoCredential;import com.mongodb.ServerAddress;import com.mongodb.client.FindIterable;import com.mongodb.client.MongoCollection;import com.mongodb.client.MongoCursor;import com.mongodb.client.MongoDatabase;public class MongoZyh {public static void main(String[] args) {try {// 連接到MongoDB服務(wù),ServerAddress()兩個參數(shù)分別為 服務(wù)器地址 和 端口ServerAddress serverAddress = new ServerAddress("localhost", 27017);List<ServerAddress> addrs = new ArrayList<ServerAddress>();addrs.add(serverAddress);// 三個參數(shù)分別為 用戶名 數(shù)據(jù)庫名稱 密碼MongoCredential credential = MongoCredential.createScramSha1Credential("zyh", "admin","zyh".toCharArray());List<MongoCredential> credentials = new ArrayList<MongoCredential>();credentials.add(credential);// 通過連接認(rèn)證獲取MongoDB連接MongoClient mongoClient = new MongoClient(addrs, credentials);// 連接到數(shù)據(jù)庫MongoDatabase mongoDatabase = mongoClient.getDatabase("zyhdb");// 新建集合,執(zhí)行后會在數(shù)據(jù)庫里新建一個空的集合// mongoDatabase.createCollection("student");// System.out.println("新建集合成功");// 獲取集合,并往集合中插入數(shù)據(jù)MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("student");// 插入一條數(shù)據(jù)// Document document = new Document();// document.append("name", "zhangsan");// document.append("age", 28);// mongoCollection.insertOne(document);// System.out.println("插入一條數(shù)據(jù)成功");// 插入多條數(shù)據(jù)// List<Document> documentList = new ArrayList<Document>();// Document document1 = new Document();// document1.append("name", "lisi");// document1.append("age", 28);// document1.append("sex", "男");// Document document2 = new Document();// document2.append("name", "wangwu");// document2.append("age", 31);// document2.append("sex", "男");// documentList.add(document1);// documentList.add(document2);// mongoCollection.insertMany(documentList);// System.out.println("插入多條數(shù)據(jù)成功");// 查詢數(shù)據(jù)// 查詢集合中所有的數(shù)據(jù)// FindIterable<Document> findIterable = mongoCollection.find();// MongoCursor<Document> mongoCursor = findIterable.iterator();// while (mongoCursor.hasNext()) {// System.out.println(mongoCursor.next());// }// 根據(jù)條件查詢// Document query = new Document();// query.put("age", new Document("$lt", 30));// query.put("sex", "男");// query.put("name", query);// 正則表達(dá)式查詢// Pattern pattern = Pattern.compile("^zhang");// query.put("name", pattern);// 排序// Document sort = new Document();// sort.put("name", -1); // 1是正序,-1是倒序// FindIterable<Document> findIterable = mongoCollection.find(query)// .sort(sort);// MongoCursor<Document> mongoCursor = findIterable.iterator();// while (mongoCursor.hasNext()) {// Document doc = mongoCursor.next();// System.out.print("name:" + doc.get("name") + "...");// System.out.print("age:" + doc.get("age") + "...");// System.out.println("sex:" + doc.get("sex") + "...");// }// mongoCollection.findOneAndUpdate(查詢條件, 修改內(nèi)容); // 查詢出第一條數(shù)據(jù)并修改// mongoCollection.findOneAndDelete(查詢條件); // 查詢出第一條數(shù)據(jù)并刪除// mongoCollection.findOneAndReplace(查詢條件, 替換內(nèi)容); // 查詢出第一條數(shù)據(jù)并替換// 修改數(shù)據(jù)// Document query = new Document();// query.put("age", 28);// Document update = new Document();// Document d = new Document();// d.put("birthday", new Date());// d.put("name", "zhangsan");// update.put("$set", d);// mongoCollection.updateOne(query, update); // 修改查詢到的第一條數(shù)據(jù)// mongoCollection.updateMany(查詢條件, 修改內(nèi)容);// 修改查詢到的所有數(shù)據(jù)// 刪除數(shù)據(jù)// Document query = new Document();// query.put("age", 28);// mongoCollection.deleteOne(query); // 刪除查詢到的第一條數(shù)據(jù)// mongoCollection.deleteMany(查詢條件); // 刪除查詢到的所有數(shù)據(jù)// mongoCollection.drop(); // 刪除集合} catch (Exception e) {e.printStackTrace();}}}

關(guān)于小編給大家介紹的MongoDB快速入門筆記(八)之MongoDB的java驅(qū)動操作代碼講解就給大家介紹這么多,希望對大家有所幫助,還會給大家持續(xù)更新MongoDB相關(guān)知識,敬請關(guān)注VEVB武林網(wǎng)網(wǎng)站!

 


注:相關(guān)教程知識閱讀請移步到MongoDB頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 日本成人二区 | 91资源在线观看 | 免费国产在线视频 | 国产精品成年片在线观看, 激情小说另类 | www.91视频com | 欧美色淫 | 艹男人的日日夜夜 | 国产成视频在线观看 | 亚洲精品一区二区三区在线看 | 久久我不卡 | 精品久久中文网址 | 日本精品久久久一区二区三区 | lutube成人福利在线观看污 | 欧美顶级毛片在线播放小说 | 国产午夜精品久久久久久久蜜臀 | 深夜精品福利 | 久久免费视频精品 | 免费视频www在线观看 | 蜜桃网站免费 | 欧美性生交xxxxx久久久 | 女人裸体让男人桶全过程 | 亚洲第五色综合网 | 7777在线观看 | 国产精选电影免费在线观看网站 | 黄网站在线免费看 | 天天看逼 | 国产网站黄 | 99999久久久久久 | 日韩精品无码一区二区三区 | 有兽焉免费动画 | 免费看操片 | 激情久久一区二区 | 日韩视频一区二区三区在线观看 | 欧洲精品久久 | 成人三级电影网站 | 成人三级在线播放 | 亚洲视频观看 | 激情视频免费看 | 色屁屁xxxxⅹ免费视频 | 久久精品影视 | 久久蜜桃香蕉精品一区二区三区 |