Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:


1. Overview

In this tutorial, we’ll compare two of the most popular NoSQL databases – MongoDB and Couchbase. We’ll look at the architecture, the list of features, the data model, querying, and how each handles partitioning.

2. Intro to NoSQL Databases

SQL databases have been around since 1970 and have been the de facto databases for quite some time. One of their purposes was to reduce data duplication, as storage wasn’t cheap in those days. Horizontal scaling implied lots of maintenance effort for SQL databases, but one could scale vertically by buying a more powerful server.

NoSQL (Not only SQL) databases came around the late 2000s and allowed for easier horizontal scaling. We could now distribute our data on many not-so-powerful machines as computing power was getting cheaper and cheaper. Data here is not stored in tables but in documents (usually in JSON format), and the schema wasn’t rigid as in the case of SQL databases.

One of the first NoSQL databases is MongoDB. Its simple setup and ease of use have kept it at the top for quite some time. However, there are new contenders like Couchbase, which comes with easy scaling and high performance.

3. Architecture

Let’s have a look at the architecture of a MongoDB deployment:

MongoDB Architecture

Here, each of the nodes contains a replica set with one primary and two secondary replicas.

Each replica has its own MongoD service, which manages data, index, and query. The MongoS components act as distributed query routers and processors.

Now let’s have a look at the Couchbase deployment architecture:

Couchbase Architecture

Each node in the Couchbase deployment has a mandatory Cluster Manager. The other service components, namely Data, Index, Query, and Search, can be present or not.

For Couchbase, data is stored in Buckets which are partitioned between the nodes using automatic hashing.

There’s no need for a config server, as metadata management is built into the database by design. Scaling out is just a matter of adding more nodes.

4. Feature Comparison

Let’s see a main features comparison of the two NoSQL databases:

Feature MongoDB Couchbase
Storage Stores documents as binary JSON (BSON) Stores documents as JSON
Querying Proprietary query language N1QL – supports SQL queries
Partitioning Handles partitioning manually by sharding Automatic partitioning using a hashing mechanism
Transactions From version 4.2 upwards supports distributed transactions From version 6.6 upwards supports distributed transactions
Search Starting with version 4.0 upwards supports text-search From version 6.0 upwards supports full-text-search

5. Data Model

Unlike SQL databases, in the case of NoSQL, the data model is not enforced. The documents aren’t required to have the same schema. We have the freedom to model each relation however we see fit.

Let’s take a look at the modeling of relationships between entities:

  • One-to-one (1-1): the default way a NoSQL database would handle this is by embedding one entity in the other. This is also the behavior of both MongoDB and Couchbase. We can also embed the entity id of the other entity. Both of the databases support this.
  • One-to-many (1-N): we solve this by either embedding the array of N entities or the array of N entity ids in the first entity. MongoDB and Couchbase support this.
  • Many-to-many (M-N): we solve this by either embedding the array of N entities in each of the M entities or the array of N entity ids in each of the M entities. MongoDB and Couchbase support this also.

The decision of which entity should be embedded in the other and the choice to embed either entities or ids is a key design decision. We take this decision after carefully considering how applications will use our database.

6. Data Querying

For querying, MongoDB has its proprietary query language, while Couchbase uses N1QL, a language that resembles SQL. Let’s take a STUDENT collection for exemplification and see how each database handles the usual command-query operations.


Here’s the SELECT operation in SQL language, MongoDB query language, and Couchbase N1QL:

-- SQL

-- MongoDB query language

-- Couchbase N1QL


Now, let’s see how the INSERT operation looks in SQL, MongoDB query language, and Couchbase N1QL:

-- SQL
INSERT INTO STUDENT(id, name) VALUES (123, 'Ryan');

-- MongoDB query language{_id: "123", {"id": "123", "name": "Ryan"})

-- Couchbase N1QL
INSERT INTO STUDENT(KEY, VALUE) VALUES ('123', {"id": "123", "name": "Ryan"})


Next is the UPDATE operation in SQL, MongoDB query language, and Couchbase N1QL:

-- SQL
UPDATE STUDENT SET name='John' WHERE id = 123;

-- MongoDB query language
db.STUDENT.update({_id: "123"}, {"name": "John"})

-- Couchbase N1QL
UPDATE STUDENT SET name = "John" WHERE id = "123"


And here’s the last of the basic operations – DELETE. Let’s see it in SQL, MongoDB query language, and Couchbase N1QL:

-- SQL

-- MongoDB query language
db.STUDENT.remove({_id: "123"})

-- Couchbase N1QL

We can get an in-depth look at MongoDB queries using Spring Data in our Spring Data MongoDB tutorial.

Also, we can learn about persisting Couchbase documents with Spring Data in our Spring Data Couchbase tutorial.

7. Partitioning

Partitioning is a key feature of any NoSQL database, and these two make no exception.

MongoDB doesn’t partition its data by default. Instead, it keeps all data on a single node. MongoDB does horizontal scaling by splitting the data into subsets (shards), and we can distribute each subset on a different node. The config server manages the configuration for the deployment cluster.

There’s no config server in Couchbase. Instead, each node has its Cluster Manager service and can run any of the Data, Index, Query, and Search services. This way, we can achieve great flexibility by distributing computing power where it’s needed.

8. Conclusion

In this article, we explored the similarities and differences between MongoDB and Couchbase.

To sum things up, MongoDB is a great way to start our journey into the NoSQL world. It has a great community around it, and MongoDB University offers lots of training for developers.

On the other hand, Couchbase supports SQL queries and promises great performance and easy scaling.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

res – Persistence (eBook) (cat=Persistence)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.