MongoDB 是一個基於分佈式文件存儲的數據庫。由 C++ 語言編寫,通常生產上建議以共享分片的形式來部署。 可是MongoDB官方也提供了其它語言的客戶端操做API。以下圖所示:java
提供了C、C++、C#、.net、GO、java、Node.js、PHP、python、scala等各類語言的版本,以下圖所示:python
MongoDB的操做分爲同步操做和異步操做以及響應式編程操做
1、同步操做APImysql
官方JAVA API的路徑:https://docs.mongodb.com/ecosystem/drivers/java/ 咱們這裏以3.11的java 版本爲例。各個版本的API對MongoDB服務的支持狀況。react
使用API時,先引入maven依賴git
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver --> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.11.1</version> </dependency>
一、關於MongoDB Client的初始化和關閉。github
從官方介紹來看,通常建議Client只須要一個創建一個長鏈接實例,而後使用時,都使用這個實例就能夠,也就是能夠用java的單例模式來建立鏈接實例。sql
//mongoClient鏈接 protected static MongoClient mongoClient; public synchronized static MongodbClient getInstance(String mongodbUrl) { if (null == mongoClient) { mongoClient = MongoClients.create(mongodbUrl); if(null != mongoClient){ log.info("mongoClient init success!"); } else{ log.info("mongoClient init failed!"); } } return mongodbClient; }
直接經過mongodb的host和port來建立client: mongodb
MongoClient mongoClient = MongoClients.create("mongodb://host1:27017");
client鏈接到一個 Replica Set:數據庫
MongoClient mongoClient = MongoClients.create("mongodb://host1:27017,host2:27017,host3:27017"); MongoClient mongoClient = MongoClients.create("mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myReplicaSet");
或者經過MongoClientSettings.builder() 來輔助生成鏈接字符串來建立client:編程
MongoClient mongoClient = MongoClients.create( MongoClientSettings.builder() .applyToClusterSettings(builder -> builder.hosts(Arrays.asList( new ServerAddress("host1", 27017), new ServerAddress("host2", 27017), new ServerAddress("host3", 27017)))) .build());
鏈接關閉:
public void close() { if(null!=mongoClient){ mongoClient.close(); mongoClient=null; } }
2、關於MongoDB 的基本操做
//建立Collection
public void createCollection(String dataBaseName,String collectionName){ getDatabase(dataBaseName).createCollection(collectionName); }
//查詢dataBaseName
public MongoDatabase getDatabase(String dataBaseName){ return mongoClient.getDatabase(dataBaseName); }
//查詢Collection
public List<String> listCollectionNames(String dataBaseName){
List<String> stringList = new ArrayList<String>();
mongoClient.getDatabase(dataBaseName).listCollectionNames().forEach((Consumer<? super String>) t->{ stringList.add(t); });
return stringList; }
public MongoCollection<Document> getCollectionByName(String dataBaseName, String collectionName){ return getDatabase(dataBaseName).getCollection(collectionName); }
3、關於MongoDB 的查詢操做
//經過id(objectid)精確查詢 public FindIterable<Document> findMongoDbDocById(String dataBaseName, String collectionName, String id){ BasicDBObject searchDoc = new BasicDBObject().append("_id", id); return getCollectionByName(dataBaseName,collectionName).find(searchDoc); } //經過id(objectid)模糊查詢 public FindIterable<Document> findMongoDbDocByIdRegex(String dataBaseName, String collectionName, String id){ BasicDBObject searchDoc = new BasicDBObject().append("_id", new BasicDBObject("$regex",id)); return getCollectionByName(dataBaseName,collectionName).find(searchDoc); } //經過開始id和結束id 查詢(根據objectId範圍查詢) public FindIterable<Document> findMongoDbDocById(String dataBaseName, String collectionName, String startId,String endId){ BasicDBObject searchDoc = new BasicDBObject().append("_id", new BasicDBObject("$gte", startId).append("$lte", endId)); return getCollectionByName(dataBaseName,collectionName).find(searchDoc); } public FindIterable<Document> findMongoDbDoc(String dataBaseName, String collectionName,BasicDBObject basicDBObject){ return getCollectionByName(dataBaseName,collectionName).find(basicDBObject); } //限制查詢返回的條數 public FindIterable<Document> findMongoDbDoc(String dataBaseName, String collectionName,BasicDBObject basicDBObject,Integer limitNum){ return findMongoDbDoc(dataBaseName,collectionName,basicDBObject).limit(limitNum) ; } public FindIterable<Document> findMongoDbDocById(String dataBaseName, String collectionName, String startId,String endId,Integer limitNum){ return findMongoDbDocById(dataBaseName,collectionName,startId,endId).limit(limitNum); } /** * 降序查詢(排序) * @param dataBaseName * @param collectionName * @param startId * @param endId * @param sortField 排序字段 * @return */ public FindIterable<Document> findMongoDbDocByIdDescSort(String dataBaseName, String collectionName, String startId,String endId,String sortField){ return findMongoDbDocById(dataBaseName,collectionName,startId,endId).sort(new Document().append(sortField, -1)); } public FindIterable<Document> findMongoDbDocByIdDescSort(String dataBaseName, String collectionName, String startId,String endId,String sortField,Integer limitNum){ return findMongoDbDocByIdDescSort(dataBaseName,collectionName,startId,endId,sortField).limit(limitNum); } /** * 降序查詢(排序) * @param dataBaseName * @param collectionName * @param startId * @param endId * @param sortField 排序字段 * @return */ public FindIterable<Document> findMongoDbDocByIdAscSort(String dataBaseName, String collectionName, String startId,String endId,String sortField){ return findMongoDbDocById(dataBaseName,collectionName,startId,endId).sort(new Document().append(sortField, 1)); } public FindIterable<Document> findMongoDbDocByIdAscSort(String dataBaseName, String collectionName, String startId,String endId,String sortField,Integer limitNum){ return findMongoDbDocByIdAscSort(dataBaseName,collectionName,startId,endId,sortField).limit(limitNum); }
4、關於MongoDB 的插入操做
//插入操做,注意插入時,若是數據已經存在會報錯,插入時必須數據不存在,不會自動進行覆蓋 //插入單條記錄 public void insertDoc(String dataBaseName, String collectionName, Document document){ getCollectionByName(dataBaseName,collectionName).insertOne(document); } //插入多條記錄 public void insertDoc(String dataBaseName, String collectionName,List<? extends Document> listData){ getCollectionByName(dataBaseName,collectionName).insertMany(listData); }
5、關於MongoDB 的更新操做
//更新單條 public void updateDoc(String dataBaseName, String collectionName, Bson var1, Bson var2){ getCollectionByName(dataBaseName,collectionName).updateOne(var1,var2); } public void updateDoc(String dataBaseName, String collectionName, Bson var1, List<? extends Bson> list){ getCollectionByName(dataBaseName,collectionName).updateOne(var1,list); } //批量更新 public void updateDocs(String dataBaseName, String collectionName, Bson var1, Bson var2){ getCollectionByName(dataBaseName,collectionName).updateMany(var1,var2); } public void updateDocs(String dataBaseName, String collectionName, Bson var1, List<? extends Bson> list){ getCollectionByName(dataBaseName,collectionName).updateMany(var1,list); }
6、關於MongoDB 的刪除操做
//單條刪除 public DeleteResult deleteDoc(String dataBaseName, String collectionName, Bson var1){ return getCollectionByName(dataBaseName,collectionName).deleteOne(var1); } //批量刪除 public DeleteResult deleteDocs(String dataBaseName, String collectionName,Bson var1){ return getCollectionByName(dataBaseName,collectionName).deleteMany(var1); }
7、關於MongoDB 的替換操做
//存在就替換,不存在的話就插入 public UpdateResult replaceDoc(String dataBaseName, String collectionName, Bson var1, Document var2){ return getCollectionByName(dataBaseName,collectionName).replaceOne(var1,var2); }
八、關於MongoDB 的bulkWrite操做 (批量寫入),對於數據不少時,效率很高
public BulkWriteResult bulkWrite(String dataBaseName, String collectionName, List<? extends WriteModel<? extends Document>> listData){ return getCollectionByName(dataBaseName,collectionName).bulkWrite(listData); }
9、關於MongoDB 的分頁查詢
mongodb的分頁查詢能夠有多種思路來實現。
思路一:採用相似mysql的limit start end 的這種。
獲取到總的數量:
//查詢總數 public long countDocs(String dataBaseName, String collectionName,Bson var1){ if(null==var1){ return getCollectionByName(dataBaseName,collectionName).countDocuments(); } return getCollectionByName(dataBaseName,collectionName).countDocuments(var1); }
// 分頁查詢,採用skip+limit的方式,在用了總數後,就能夠分頁了,skip的意思是前面跳過多少數據。可是這種方式在數據量大的時候效率不高,由於skip會致使全表掃描。
public FindIterable<Document> findMongoDbDoc(String dataBaseName, String collectionName,BasicDBObject basicDBObject,Integer skip,Integer limit){ return getCollectionByName(dataBaseName,collectionName).find(basicDBObject).skip(skip).limit(limit); }
思路二:利用limit 以及排序的方式,獲取分頁的上一頁的最後一條記錄的objectId,而後使用排序+$gte操做(大於)+limit 來獲取當頁的數據。找到一個能夠排序的字段,好比objectId或者時間字段均可以排序。這個也是mongodb官方推薦的方式,這種作飯能夠避免全表掃描。
思路三:在數據量不大的時候,使用代碼進行分頁。好比從mongodb中查詢出一個list對象後,對list對象作代碼分頁。
public class ListUtil { public static List getPagingList(List list,Integer start,Integer length){ start = start<0?0:start; //默認爲10 length = length<=0?10:length; Integer size = list.size(); if(start>size){ start = size; } Integer toIndex = (start+length-1)>=size?size:(start+length-1); if(toIndex<=0){ toIndex = size; } return list.subList(start,toIndex); }
2、異步操做API
mongodb異步驅動程序提供了異步api,能夠利用netty或java 7的asynchronoussocketchannel實現快速、無阻塞的i/o,maven依賴
<dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-async</artifactId> <version>3.11.1</version> </dependency> </dependencies>
官方地址:http://mongodb.github.io/mongo-java-driver/3.11/driver-async/getting-started/installation/
異步操做必然會涉及到回調,回調時採用ResultCallback<Document>
SingleResultCallback<Document> callbackPrintDocuments = new SingleResultCallback<Document>() { @Override public void onResult(final Document document, final Throwable t) { System.out.println(document.toJson()); } }; SingleResultCallback<Void> callbackWhenFinished = new SingleResultCallback<Void>() { @Override public void onResult(final Void result, final Throwable t) { System.out.println("Operation Finished!"); } };
異步insert操做
collection.insertMany(documents, new SingleResultCallback<Void>() { @Override public void onResult(final Void result, final Throwable t) { System.out.println("Documents inserted!"); } });
異步刪除操做
collection.deleteMany(gte("i", 100), new SingleResultCallback<DeleteResult>() { @Override public void onResult(final DeleteResult result, final Throwable t) { System.out.println(result.getDeletedCount()); } });
異步更新操做
collection.updateMany(lt("i", 100), inc("i", 100), new SingleResultCallback<UpdateResult>() { @Override public void onResult(final UpdateResult result, final Throwable t) { System.out.println(result.getModifiedCount()); } });
異步統計操做
collection.countDocuments( new SingleResultCallback<Long>() { @Override public void onResult(final Long count, final Throwable t) { System.out.println(count); } });
3、MongoDB Reactive Streams 操做API
官方的MongoDB reactive streams Java驅動程序,爲MongoDB提供異步流處理和無阻塞處理。
徹底實現reactive streams api,以提供與jvm生態系統中其餘reactive streams的互操做,通常適合於大數據的處理,好比spark,flink,storm等。
<dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-reactivestreams</artifactId> <version>1.12.0</version> </dependency> </dependencies>
官方地址:http://mongodb.github.io/mongo-java-driver-reactivestreams/
會包含以下三部分:
API問的地址:http://mongodb.github.io/mongo-java-driver-reactivestreams/1.12/javadoc/
代碼示例:
//創建鏈接 MongoClient mongoClient = MongoClients.create(mongodbUrl); //得到數據庫對象 MongoDatabase database = client.getDatabase(databaseName); //得到集合 MongoCollection collection = database.getCollection(collectionName); //異步返回Publisher FindPublisher publisher = collection.find(); //訂閱實現 publisher.subscribe(new Subscriber() { @Override public void onSubscribe(Subscription str) { System.out.println("start..."); //執行請求 str.request(Integer.MAX_VALUE); } @Override public void onNext(Document document) { //得到文檔 System.out.println("Document:" + document.toJson()); } @Override public void onError(Throwable t) { System.out.println("error occurs."); } @Override public void onComplete() { System.out.println("finished."); } });