在mongodb中,表(Table)被稱之為集合(Collection),記錄(Record)被稱為文檔(Document)
首先連接到數據庫
MongoClient mongoClient = new MongoClient();
MongoDatabase db = mongoClient.getDatabase("test");
得到集合,這里取"text"
MongoCollection<Document> doc = db.getCollection("text");
插入一個document
doc.insertOne(new Document("name","張三").append("age", 20));
在執行insertOne方法的時候,如果database中不存在對應的名為"text"的集合,則自動創建這個collection,