標籤:name 本地 sni tca _id href tables crud base
上一篇我們講了MongoDB 的命令入門初探,本篇blog將基於上一篇blog所建立的資料庫和表完畢一個簡單的Java MongoDB CRUD Example,利用Java串連MongoDB資料庫。並實現建立資料庫、擷取表、遍曆表中的對象、對錶中對象進行CRUD操作等常式。
1、下載MongoDB Java 支援驅動包
【gitHub】https://github.com/mongodb/mongo-java-driver/downloads
2、建立Javaproject,並匯入jar包
3、串連本機資料庫server
在控制台中開啟Mongodb服務,詳細操作可參考【MongoDB資料庫】怎樣安裝、配置MongoDB
try {mongo = new MongoClient("localhost", 27017);// 保證MongoDB服務已經啟動db = mongo.getDB("andyDB");// 擷取到資料庫} catch (UnknownHostException e) {e.printStackTrace();}
3、遍曆全部的資料庫名
public class DBConnection extends TestCase {private MongoClient mongo;private DB db ;@Overrideprotected void setUp() throws Exception {// TODO Auto-generated method stubsuper.setUp();try {mongo = new MongoClient("localhost", 27017);// 保證MongoDB服務已經啟動db = mongo.getDB("andyDB");// 擷取到資料庫andyDB} catch (UnknownHostException e) {e.printStackTrace();}}public void testGetAllDB() {List<String> dbs = mongo.getDatabaseNames();// 擷取到全部的資料庫名for (String dbname : dbs) {System.out.println(dbname);}}}
4、擷取到指定資料庫
DB db = mongo.getDB("andyDB");// 擷取到資料庫
5、遍曆資料庫中全部的表名
在DBConnection測試類中加入例如以下測試方法就可以:
public void testGetAllTables() {Set<String> tables = db.getCollectionNames();for (String coll : tables) {System.out.println(coll);}}
6、擷取到指定的表
DBCollection table = db.getCollection("person");
7、遍曆表中全部的對象
public void testFindAll(){DBCollection table = db.getCollection("person");DBCursor dbCursor = table.find();while(dbCursor.hasNext()){DBObject dbObject = dbCursor.next();//列印該對象的特定欄位資訊System.out.println("name:"+dbObject.get("name")+",age:"+dbObject.get("age"));//列印該對象的全部資訊System.out.println(dbObject);}}
Console表單列印訊息:
name:jack,age:50.0
{ "_id" : { "$oid" : "537761da2c82bf816b34e6cf"} , "name" : "jack" , "age" : 50.0}
name:小王,age:24
{ "_id" : { "$oid" : "53777096d67d552056ab8916"} , "name" : "小王" , "age" : 24}
8、儲存對象
1)儲存對象方法一
public void testSave() {DBCollection table = db.getCollection("person");BasicDBObject document = new BasicDBObject();document.put("name", "小郭");// 能直接插入漢字document.put("age", 24);//"age"相應的值是int型table.insert(document);}在mongodb shell中使用命令查看資料
> db.person.find()
{ "_id" : ObjectId("537761da2c82bf816b34e6cf"), "name" : "jack", "age" : 50 }
{ "_id" : ObjectId("53777096d67d552056ab8916"), "name" : "小王", "age" : 24 }
{ "_id" : ObjectId("5377712cd67d84f62c65c4f6"), "name" : "小郭", "age" : 24 }
2)儲存對象方法二
public void testSave2() {DBCollection table = db.getCollection("person");BasicDBObject document = new BasicDBObject();//能夠加入多個欄位document.put("name", "小張");// 能直接插入漢字document.put("password", "xiaozhang");// 多加入一個欄位也是能夠的,由於MongoDB儲存的是對象。document.put("age", "23");//"age"相應的值是Stringtable.insert(document);}
在mongodb shell中使用命令查看資料
> db.person.find()
{ "_id" : ObjectId("537761da2c82bf816b34e6cf"), "name" : "jack", "age" : 50 }
{ "_id" : ObjectId("53777096d67d552056ab8916"), "name" : "小王", "age" : 24 }
{ "_id" : ObjectId("5377712cd67d84f62c65c4f6"), "name" : "小郭", "age" : 24 }
{ "_id" : ObjectId("53777230d67dfe576de5079a"), "name" : "小張", "password" : "xiaozhang", "age" : "23" }
3)儲存對象方法三(通過加入Map集合的方式加入資料到BasicDBObject)
public void testSave3(){DBCollection table = db.getCollection("person");Map<String,Object> maps = new HashMap<String,Object>();maps.put("name", "小李");maps.put("password", "xiaozhang");maps.put("age", 24);BasicDBObject document = new BasicDBObject(maps);//這樣加入後。對象裡的欄位是無序的。table.insert(document);}
在mongodb shell中使用命令查看資料
> db.person.find()
{ "_id" : ObjectId("537761da2c82bf816b34e6cf"), "name" : "jack", "age" : 50 }
{ "_id" : ObjectId("53777096d67d552056ab8916"), "name" : "小王", "age" : 24 }
{ "_id" : ObjectId("5377712cd67d84f62c65c4f6"), "name" : "小郭", "age" : 24 }
{ "_id" : ObjectId("53777230d67dfe576de5079a"), "name" : "小張", "password" : "xiaozhang", "age" : "23" }
{ "_id" : ObjectId("537772e9d67df098a26d79a6"), "age" : 24, "name" : "小李", "password" : "xiaozhang" }
9、更新對象
我們能夠結合【mongodb shell命令】db.person.update({name:"小李"},{$set:{password:"hello"}})來理解Java是怎樣操作對象來更新的。{name:"小李"}是一個BasicDBObject。{$set:{password:"hello"}也是一個BasicDBObject。這樣理解的話。你就會認為mongodb shell命令操作和Java操作非常相似。
public void testUpdate() {DBCollection table = db.getCollection("person");BasicDBObject query = new BasicDBObject();query.put("name", "小張");BasicDBObject newDocument = new BasicDBObject();newDocument.put("age", 23);BasicDBObject updateObj = new BasicDBObject();updateObj.put("$set", newDocument);table.update(query, updateObj);}// 命令操作:db.person.update({name:"小李"},{$set:{password:"hello"}})public void testUpdate2() {DBCollection table = db.getCollection("person");BasicDBObject query = new BasicDBObject("name", "小張");BasicDBObject newDocument = new BasicDBObject("age", 24);BasicDBObject updateObj = new BasicDBObject("$set", newDocument);table.update(query, updateObj);}
10、刪除對象
可參考db.users.remove({name:"小李"})命令來理解Java操作對象
public void testDelete(){DBCollection table = db.getCollection("person");BasicDBObject query = new BasicDBObject("name", "小李");table.remove(query);}
11、參考
Java + MongoDB Hello World Example(推薦)
12、你可能感興趣
【MongoDB資料庫】怎樣安裝、配置MongoDB
【MongoDB資料庫】MongoDB 命令入門初探
轉載請註明出處:http://blog.csdn.net/andie_guo/article/details/26098331,謝謝!
【MongoDB資料庫】Java MongoDB CRUD Example