以下直接轉貼官方的範例。
連接 MongoDB
1 2 3 |
Mongo m = new Mongo(); DB db = m.getDB( "mydb" ); DBCollection coll = db.getCollection( "testCollection" ); |
有需要的時候可以自行指定位址和連接埠,另外也可以用 ServerAddress 的方式來表達多個 MongoDB 的位址(用於 sharding)。
初始化 Mongo 的實體之後,可以使用 getDB() 來指定要使用的資料庫名稱,然後再使用 getCollection() 選定目前要存取的集合名稱。
新增資料到 MongoDB
在 Java 中要對 MongoDB 下指令,通常都會使用 BasicDBObject 這個物件來作為指令
這個物件其實格式就是 JSON 的格式,因為 MongoDB 都是直接使用 JSON 來下指令的。
舉例來說,如果要插入的資料是以下的資料:
1 2 3 4 5 6 7 8 9 |
{ "name" : "MongoDB" , "type" : "database" , "count" : 1 "info" : { "x" : 203 , "y" : 102 } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
BasicDBObject doc = new BasicDBObject(); doc.put( "name" , "MongoDB" ); doc.put( "type" , "database" ); doc.put( "count" , 1 ); // Info BasicDBObject info = new BasicDBObject(); info.put( "x" , 203 ); info.put( "y" , 102 ); doc.put( "info" , info); // Insert into collection coll.insert(doc); |
在 MongoDB 上搜尋資料
搜尋資料的方法也是一樣的,一樣是透過 BasicDBObject 來操作。
1 2 3 4 5 6 7 8 9 10 11 |
BasicDBObject query = new BasicDBObject(); query.put( "i" , 71 ); cursor = coll.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } |
1 |
db.testCollection.find({i:71}); |
如果需要的是數字比較,或者是要用 Regular Expression,可以參考 [2] 的說明
有講解到在 MongoDB 裡面要如何使用更進階的 Query 方法。
在 MongoDB 上建立索引
雖然說 MongoDB 不用建索引,速度也是快得有點嚇人(可以看後面的測試...)
不過當然一般狀況下還是推薦要建立索引,畢竟速度這種東西沒有人嫌慢的!XD
註:資料量已經很多的時候才去建索引時,MongoDB 會花上比較長時間建立,並且在建立期間會鎖住整個資料庫。
如果需要維持 MongoDB 的服務持續運作,在建立索引時必須加入 background: true 屬性來要求在背景建立。
建立索引的說明可以參考官方文件 [3],基本的索引的建立方法很簡單~指令的下法一樣是透過 BasicDBObject
1 2 3 |
BasicDBObject indexDbo = new BasicDBObject(); indexDbo.append( "path" , 1 ); coll.ensureIndex(indexDbo); |
1 |
db.testCollection.ensureIndex({path:1}); |
而順序的部份,依據 [3] 的說明節錄如下:
When creating an index, the number associated with a key specifies the direction of the index, so it should always be 1 (ascending) or -1 (descending). Direction doesn't matter for single key indexes or for random access retrieval but is important if you are doing sorts or range queries on compound indexes.
可以看出索引的屬性只有 1 或 -1,用來表示「遞增」或「遞減」,會影響到需要做排序或者對某個範圍搜尋的結果。
參考資料:
1、MongoDB:Java Tutorial
2、MongoDB:Advanced Queries
3、MongoDB:Indexes
4、How to query mongodb with “like” using the java api?
5、MongoDB regex, I get a different answer from the Java API compared with the console
沒有留言:
張貼留言