亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:115日期:2023-03-30 13:21:18

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

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

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服務,ServerAddress()兩個參數(shù)分別為 服務器地址 和 端口
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);
// 通過連接認證獲取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);
// 正則表達式查詢
// 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(查詢條件, 修改內容); // 查詢出第一條數(shù)據(jù)并修改
// mongoCollection.findOneAndDelete(查詢條件); // 查詢出第一條數(shù)據(jù)并刪除
// mongoCollection.findOneAndReplace(查詢條件, 替換內容); // 查詢出第一條數(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(查詢條件, 修改內容);// 修改查詢到的所有數(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();
}
}
}

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

標簽: MariaDB
相關文章:
主站蜘蛛池模板: 欧美在线视频一区二区三区 | 国产精品第四页 | 亚洲欧美色综合一区二区在线 | 免费一级毛片在线观看 | 日韩高清性爽一级毛片免费 | 国产伦码精品一区二区 | 亚洲国产成人久久77 | 国产精品久久一区二区三区 | 福利入口在线观看 | 久久在线视频免费观看 | 在线观看视频色 | 国产亚洲精品资源一区 | 全黄一级裸片视频在线观看 | 91尤物在线视频 | 国精产品一区一区三区 | 亚洲+自拍+色综合图区一 | 久久久久久久九九九九 | 国产日本高清 | 国产婷婷色综合成人精品 | chinese在线播放91国内 | 免费毛片a| 欧美一区二区三区在线播放 | 亚洲国产欧美一区二区欧美 | 免费国产成人高清视频网站 | 亚洲在线日韩 | 免费观看a毛片一区二区不卡 | 精品日韩欧美国产一区二区 | 91麻豆免费观看 | 刺激花蒂抽搐视频在线看 | 美国一级视频 | 特黄特色大片免费播放 | 亚洲综合伊人色一区 | 亚洲视频在线观看免费 | 国产成人18黄网站在线观看网站 | 日本一级毛片在线观看 | 91最新视频在线观看 | a级国产片免费观看 | 最新99国产成人精品视频免费 | 免费人成网站尤物在线观看 | 精品三级国产 | 国产精品日韩欧美一区二区 |