BKJIA: Both MongoDB and CouchDB are document-oriented databases, both of which use JSON document formats. They are generally seen as NoSQL databases and are very fashionable nowadays, there are many commonalities, but the difference between the two is obvious when it comes to queries. CouchDB needs a predefined view which is essentially a JavaScript MapReduce function ), mongoDB supports dynamic queries, similar to ad hoc queries on traditional relational databases.) More importantly, when it comes to queries, CouchDB APIs are RESTful while MongoDB APIs are more native, this means that a driver is required to issue a query in the code.
For example, when using CouchDB, some external tools such as Groovy RESTClient can be used to insert some data:
- 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"])
Note: In this case, I must specify a ticket stop number of 1234334325). By the way, you can also require CouchDB to use UUID, for example, an http get request is sent to the/_ uuids path.
BKJIA editor recommends: Strong mango: Entering MongoDB
For example, if I want to find out all the tickets issued by Officer Grey, I must define a view that is a simple URL for executing JavaScript MapReduce functions, therefore, I can quickly implement a function to extract all documents whose officer attribute is equal to Robert Grey.
- function(doc) {
- if(doc.officer == "Robert Grey"){
- emit(null, doc);
- }
- }
I must give this view a name. When I send an http get request to this view, I can GET at least one document.
- 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"
- }
In general, when using CouchDB, I cannot quickly issue an ad hoc RESTful call to query information. A query must be defined first, also called a view), and then exposed. On the contrary, when using MongoDB, it is no different from most relational databases. You can query any information you want to see at runtime.
For example, the following is an example of parking ticket that I implemented using MongoDB's native Java DRIVER:
- 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);
Suppose I want to query the parking ticket issued by Robert Smith in the future, just modify the officer attribute value, for example:
- BasicDBObject query = new BasicDBObject();
- query.put("officer", "Robert Smith");
- DBCursor cur = coll.find(query);
- while (cur.hasNext()) {
- System.out.println(cur.next());
- }
Although MongoDB and CouchDB have many similarities, there are indeed essential differences in queries. CouchDB needs to use MapReduce, while MongoDB is more oriented to dynamic queries, of course, MongoDB also supports MapReduce.
Original article title: MongoDB and CouchDB: vastly different queries
Additional reading
If you want to know