基于Java開發完整示例
更新時間 2023-08-29 15:14:30
最近更新時間: 2023-08-29 15:14:30
分享文章
本頁介紹了基于Java開發文檔數據庫服務應用程序的完整示例。
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.CreateCollectionOptions;
import com.mongodb.client.model.Indexes;
import org.bson.Document;
import java.util.ArrayList;
import java.util.List;
import static com.mongodb.client.model.Filters.eq;
public class MongoDBExample {
public static void main(String[] args) {
//用戶名
String username = "";
//數據庫
String databaseName = "";
//密碼
String password = "";
//連接地址
String host = "";
//端口
int port = 8030;
// 創建MongoCredential對象
MongoCredential credential = MongoCredential.createCredential(username, databaseName, password.toCharArray());
// 創建MongoClientOptions對象
MongoClientOptions options = MongoClientOptions.builder()
.retryWrites(true)
.build();
// 創建MongoClient實例
MongoClient mongoClient = new MongoClient(new ServerAddress(host, port), credential, options);
//訪問database
MongoDatabase memberInfoDatabase = mongoClient.getDatabase("MemberInfo");
//訪問collection
MongoCollection<Document> goldMemberCollection = memberInfoDatabase.getCollection("gold_member");
//創建collection
memberInfoDatabase.createCollection("testCollection", new CreateCollectionOptions().sizeInBytes(200000));
//插入數據
Document doc0 = new Document("name", "萬三")
.append("age", 23)
.append("sex", "male");
Document doc1 = new Document("name", "劉亞")
.append("age", 42)
.append("sex", "male");
Document doc2 = new Document("name", "王瑩")
.append("age", 22)
.append("sex", "female");
List<Document> documents = new ArrayList<>();
documents.add(doc0);
documents.add(doc1);
documents.add(doc2);
goldMemberCollection.insertMany(documents);
//刪除數據
goldMemberCollection.deleteOne(eq("name", "劉亞"));
//刪除表
MongoCollection<Document> collection = memberInfoDatabase.getCollection("test");
collection.drop();
//讀數據
MongoCursor<String> cursor = (MongoCursor<String>) goldMemberCollection.find();
while (cursor.hasNext()) {
Object result = cursor.next();
}
cursor.close();
//帶過濾條件的查詢
MongoCursor<String> cursorCondition = (MongoCursor<String>)goldMemberCollection.find(
new Document("name","zhangsan")
.append("age", 5));
while (cursorCondition.hasNext()) {
Object result = cursorCondition.next();
}
cursorCondition.close();
//運行命令
MongoClient mongoClientShell = (MongoClient) MongoClients.create();
MongoDatabase database = mongoClientShell.getDatabase("MemberInfo");
Document buildInfoResults = database.runCommand(new Document("buildInfo", 1));
System.out.println(buildInfoResults.toJson());
Document collStatsResults = database.runCommand(new Document("collStats", "restaurants"));
System.out.println(collStatsResults.toJson());
//創建索引
MongoCollection<Document> collectionTest = memberInfoDatabase.getCollection("gold_member");
collectionTest.createIndex(Indexes.ascending("age"));
}
}