NoSQL Civil War: Comparison of MongoDB and COUCHDB query methods

Source: Internet
Author: User
Keywords nbsp can contrast Civil war HTTP

"51CTO classic" MongoDB and Couchdb are both document-oriented databases that use JSON document formats that are often viewed as NoSQL databases and are now fashionable and have a lot in common, but when it comes to queries, the difference is obvious, COUCHDB requires predefined views (essentially JavaScript mapreduce functions), while MONGODB supports dynamic queries (essentially similar to ad hoc queries on traditional relational databases), and more importantly, when it comes to queries, the COUCHDB API is restful, The MongoDB API is more native, which means that a query is made in code using a driver.

For example, when using COUCHDB, some external tools, such as Groovy's 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", of Fense: "Parked in no Parking zone", Date: "2010/07/31"]

Note that in this case, I must specify a number (1234334325) for the parking ticket, and, incidentally, require couchdb to use a UUID, such as an HTTP GET request to the/_uuids path.

51CTO editors recommend: Strong mango: Into MongoDB

For example, if I wanted to find out all the tickets made by Officer Grey, I had to define a view that was a simple URL to execute the JavaScript mapreduce function, so I could quickly implement a function to extract the Officer property equal to Robert All of Grey's documents.

function (DOC) {if (Doc.officer = = "Robert Grey") {Emit (null, DOC); }  } 

I have to give this view a name, and when I send an HTTP GET request to this view, I can at least obtain a 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 can't quickly send out an ad hoc restful call query information, you must first define a query (also called a view), and then expose it. Conversely, when using MongoDB, it is not much different from most relational databases, and you can query for any information you want to see at run time.

For example, here is a parking ticket instance 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);

Assuming that later I want to check the parking ticket issued by Robert Smith, simply modify the officer attribute value, such as:

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, but in the query aspect does have the essential difference, COUCHDB needs to use MapReduce, but MongoDB is more dynamic-oriented query, of course, MongoDB is to support MapReduce.

Original title: MongoDB and couchdb:vastly different queries

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.