NoSQL Civil War: Comparison between MongoDB and CouchDB query methods

Source: Internet
Author: User
Tags couchdb

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:

 
 
  1. import static groovyx.net.http.ContentType.JSON  
  2. import groovyx.net.http.RESTClient  
  3.    
  4. def client = new RESTClient("http://localhost:5498/")  
  5. response = client.put(path: "parking_tickets/1234334325",  
  6.   contentType: JSON,  
  7.   requestContentType:  JSON,  
  8.   body: [officer: "Robert Grey",  
  9.          location: "199 Castle Dr",  
  10.          vehicle_plate: "New York 77777",  
  11.          offense: "Parked in no parking zone",  
  12.          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.

 
 
  1. function(doc) {  
  2.   if(doc.officer == "Robert Grey"){  
  3.     emit(null, doc);  
  4.   }  

I must give this view a name. When I send an http get request to this view, I can GET at least one document.

 
 
  1. response = client.get(path: "parking_tickets/_view/by_name/officer_grey",  
  2.         contentType: JSON, requestContentType: JSON)  
  3.    
  4. assert response.data.total_rows == 1  
  5. response.data.rows.each{  
  6.    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:

 
 
  1. DBCollection coll = db.getCollection("parking_tickets");  
  2. BasicDBObject doc = new BasicDBObject();  
  3.    
  4. doc.put("officer", "Robert Grey");  
  5. doc.put("location", "199 Castle Dr");  
  6. doc.put("vehicle_plate", "New York 77777");  
  7. //...  
  8. 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:

 
 
  1. BasicDBObject query = new BasicDBObject();  
  2. query.put("officer", "Robert Smith");  
  3. DBCursor cur = coll.find(query);  
  4.  while (cur.hasNext()) {  
  5.    System.out.println(cur.next());  
  6.  } 

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.