NoSQL內戰(zhàn):MongoDB與CouchDB查詢方式對比
原創(chuàng)【51CTO經(jīng)典譯文】MongoDB和CouchDB都是面向文檔的數(shù)據(jù)庫,它們都使用JSON文檔格式,它倆通常都被看作是NoSQL數(shù)據(jù)庫,并且現(xiàn)在都很時髦,有很多的共同點,但談到查詢時,兩者的差別就很明顯了,CouchDB需要預定義視圖(本質上是JavaScript MapReduce函數(shù)),而MongoDB支持動態(tài)查詢(基本上和傳統(tǒng)關系數(shù)據(jù)庫上的即席查詢類似),更重要的是,談到查詢時,CouchDB的API是RESTful,而MongoDB的API更原生化,這意味著在代碼中發(fā)出一個查詢就要使用一個驅動。
例如,使用CouchDB時,為了插入一些數(shù)據(jù),可以使用一些外部工具,如Groovy的RESTClient:
- import static groovyx.net.http.ContentType.JSON
- import groovyx.net.http.RESTClient
- def client = new RESTClient("http://localhost:5498/")
- response = client.put(path: "parking_tickets/1234334325",
- contentType: JSON,
- requestContentType: JSON,
- body: [officer: "Robert Grey",
- location: "199 Castle Dr",
- vehicle_plate: "New York 77777",
- offense: "Parked in no parking zone",
- date: "2010/07/31"])
注意,在這種情況下,我必須為停車票指定一個編號(1234334325),順便提一下,也可以要求CouchDB使用UUID,如向/_uuids路徑發(fā)出一個HTTP GET請求。
51CTO編輯向您推薦:強勢的芒果:走進MongoDB
例如,如果我想找出由Officer Grey開出的所有票,我必須定義一個視圖,視圖是執(zhí)行JavaScript MapReduce函數(shù)的簡單URL,因此我可以快速實現(xiàn)一個函數(shù)來提取officer屬性等于Robert Grey的所有文檔。
- function(doc) {
- if(doc.officer == "Robert Grey"){
- emit(null, doc);
- }
- }
我必須給這個視圖取一個名字,當我向這個視圖發(fā)出HTTP GET請求時,至少可以得到一個文檔。
- response = client.get(path: "parking_tickets/_view/by_name/officer_grey",
- contentType: JSON, requestContentType: JSON)
- assert response.data.total_rows == 1
- response.data.rows.each{
- assert it.value.officer == "Robert Grey"
- }
總的來說,使用CouchDB時,我不能很快地發(fā)出一個即席RESTful調用查詢信息,必須先定義一個查詢(也叫視圖),然后將其暴露出來。相反,使用MongoDB時,它和大多數(shù)關系數(shù)據(jù)庫沒多大區(qū)別,你可以在運行時查詢你想要看到的任何信息。
例如,下面是我使用MongoDB的原生Java驅動實現(xiàn)的停車票實例:
- DBCollection coll = db.getCollection("parking_tickets");
- BasicDBObject doc = new BasicDBObject();
- doc.put("officer", "Robert Grey");
- doc.put("location", "199 Castle Dr");
- doc.put("vehicle_plate", "New York 77777");
- //...
- coll.insert(doc);
假設以后我要查詢Robert Smith發(fā)出的停車票,只需要簡單修改一下officer屬性值就可以了,如:
- BasicDBObject query = new BasicDBObject();
- query.put("officer", "Robert Smith");
- DBCursor cur = coll.find(query);
- while (cur.hasNext()) {
- System.out.println(cur.next());
- }
雖然MongoDB和CouchDB有很多相似之處,但在查詢方面的確有著本質的不同,CouchDB需要使用MapReduce,而MongoDB更多的是面向動態(tài)的查詢,當然MongoDB也是支持MapReduce的。
原文標題:MongoDB and CouchDB: vastly different queries
延伸閱讀
您如果想了解更多關于數(shù)據(jù)庫查詢方面的資料,51CTO向您推薦《構建高效MySQL查詢》與《數(shù)據(jù)庫之索引與查詢專題》
【編輯推薦】