1.編寫 Java 代碼連接配接 MongoDB 資料庫。
2.編寫 Java 代碼在 MongoDB 中建立集合。
3.編寫 Java 代碼在 MongoDB 中擷取集合。
4.編寫 Java 代碼在 MongoDB 中插入文檔。
5.編寫 Java 代碼在 MongoDB 中檢索所有文檔。
6.編寫 Java 代碼在 MongoDB 中更新文檔。
7.編寫 Java 代碼在 MongoDB 中删除文檔。
package DBtest;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
public class TestMongoDB {
/**
* @param args
*/
public static void main(String[] args) {
// insert();//插入資料。執行插入時,可将其他三句函數調用語句注釋,下同
find(); //查找資料
// update();//更新資料
// delete();//删除資料
}
/**
* 傳回指定資料庫中的指定集合
* @param dbname 資料庫名
* @param collectionname 集合名
* @return
*/
//MongoDB無需預定義資料庫和集合,在使用的時候會自動建立
public static MongoCollection<Document> getCollection(String dbname,String collectionname){
//執行個體化一個mongo用戶端,伺服器位址:localhost(本地),端口号:27017
MongoClient mongoClient=new MongoClient("192.168.223.133",27017);
//執行個體化一個mongo資料庫
MongoDatabase mongoDatabase = mongoClient.getDatabase(dbname);
//擷取資料庫中某個集合
MongoCollection<Document> collection = mongoDatabase.getCollection(collectionname);
return collection;
}
/**
* 插入資料
*/
public static void insert(){
try{
//連接配接MongoDB,指定連接配接資料庫名,指定連接配接表名。
MongoCollection<Document> collection= getCollection("test","test"); //資料庫名:School 集合名:student
//執行個體化一個文檔,文檔内容為{sname:'Mary',sage:25},如果還有其他字段,可以繼續追加append
Document doc1=new Document("sname","Mary").append("sage", 25);
//執行個體化一個文檔,文檔内容為{sname:'Bob',sage:20}
Document doc2=new Document("sname","Bob").append("sage", 20);
List<Document> documents = new ArrayList<Document>();
//将doc1、doc2加入到documents清單中
documents.add(doc1);
documents.add(doc2);
//将documents插入集合
collection.insertMany(documents);
System.out.println("插入成功");
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
/**
* 查詢資料
*/
public static void find(){
try{
MongoCollection<Document> collection = getCollection("test","test"); //資料庫名:School 集合名:student
//通過遊标周遊檢索出的文檔集合
// MongoCursor<Document> cursor= collection.find(new Document("sname","Mary")). projection(new Document("sname",1).append("sage",1).append("_id", 0)).iterator(); //find查詢條件:sname='Mary'。projection篩選:顯示sname和sage,不顯示_id(_id預設會顯示)
//查詢所有資料
MongoCursor<Document> cursor= collection.find().iterator();
while(cursor.hasNext()){
System.out.println(cursor.next().toJson());
}
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
/**
* 更新資料
*/
public static void update(){
try{
MongoCollection<Document> collection = getCollection("test","test"); //資料庫名:School 集合名:student
//更新文檔将文檔中sname='Mary'的文檔修改為sage=22
collection.updateMany(Filters.eq("sname", "Mary"), new Document("$set",new Document("sage",22)));
System.out.println("更新成功!");
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
/**
* 删除資料
*/
public static void delete(){
try{
MongoCollection<Document> collection = getCollection("School","student"); //資料庫名:School 集合名:student
//删除符合條件的第一個文檔
collection.deleteOne(Filters.eq("test", "test"));
//删除所有符合條件的文檔
//collection.deleteMany (Filters.eq("sname", "Bob"));
System.out.println("删除成功!");
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
插入資料
查找資料
更新資料
删除資料