• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:9 mins read
You are currently viewing Cassandra Vs MongoDB Differences

Both Cassandra and MongoDB are NoSQL databases, and they have numerous similarities as well as differences. MongoDB is frequently used for web and mobile apps that need short development cycles and flexible data models. Cassandra employs applications with a lot of users and high write-throughput. With this article, we will get to know about both of these databases and the differences in the CRUD operation.

Advertisements

Related: MongoDB Vs PostgreSQL Differences

CRUD Operations in MongoDB vs Cassandra

In both these databases, we can perform CRUD (Create, Read, Update, Delete) operations but the syntax differs.

CRUDMongoDBCassandra
Createdb.createCollection()CREATE TABLE <TABLE>
Insertdb.collection.insertMany()INSERT INTO <TABLE>
Selectdb.collection.find()SELECT * FROM <TABLE>
Updatedb.collection.updateOne()UPDATE <TABLE>
Deletedb.collection.deleteOne()DELETE FROM <TABLE>
Cassandra vs MongoDB

For more examples of MongoDB refer to MongoDB Beginners Tutorial with Examples.

1. Data Model

Cassandra is based on a wide-column data model and it is NoSQL database, while MongoDB is a document data model. In Cassandra, data is assembled into tables with rows and columns, and the MongoDB data is stored as JSON-like documents.

2. Scalability

Both databases are designed to scale horizontally, but Cassandra is particularly well-suited for large-scale, distributed deployments. Also, the decentralized architecture of Cassandra allows it to handle massive amounts of data across multiple data centers, while MongoDB is typically used in smaller, more centralized deployments.

3. Performance

Cassandra and MongoDB, both are known for their high performance, but Cassandra is generally faster when handling large amounts of data, while MongoDB is faster for smaller datasets.

4. Consistency

Cassandra has a tunable consistency model, where we can choose between strong or eventual consistency, while MongoDB has strong consistency by default.

5. CREATE in Cassandra Vs MongoDB

In Cassandra, the data can be added to a table by inserting a new row or updating an existing row. Like the following query.

Note that Cassandra uses a query language called CQL (Cassandra Query Language), which is similar to SQL but optimized for NoSQL databases.


# INSERT in Cassandra
INSERT INTO student (id, name, age)   
VALUES(1,'Nick', 22);  
INSERT INTO student (id, name, age)   
VALUES(2,'Marry', 25);  
INSERT INTO student (id, name, age)   
VALUES(3,'John', 21);

MongoDB uses a query language called MongoDB Query Language (MQL), which is designed to work with documents. Here, new data is added to a collection by inserting a new document. By using insertMany() query you can insert multiple documents at a time into the collection.


# Creating Collection in MongoDB
db.student.insertMany([
      {  
         _id: 1,
         name: "Jimmy",
         age: 24
 
      },
      {
         _id: 2,
         name: "Elena",
         age: 20
      },
      {
         _id: 3,
         name: "Caroline",
         age: 25
      }
   ]
)

Hence, the data are inserted and returned the following unique inserted Ids for every document.

Cassandra Vs MongoDB

6. READ in Cassandra Vs MongoDB

In both databases, we can read the data using a query. However, Cassandra’s query language (CQL) is more limited than MongoDB’s query language.

In Casandra, you can read the table data with the SELECT * FROM command.


# Read the table data in Cassandra 
SELECT * FROM student;

Following is a retrieval of the table’s structure and contents.

Cassandra Vs MongoDB difference

Conversely, MongoDB supports more advanced querying and aggregation operations for retrieving the data. Here, we have read the data using the find() method of MongoDB.


# find() method of MongoDB
db.student.find()

Thus, the output displayed the following data of the MongoDB collection.

If you want to find records where the array field is empty in MongoDB, you can use the following link.

7. UPDATE in Cassandra Vs MongoDB

In Cassandra, we update the data by inserting a new row with the updated values or by updating the existing row with new data. The update command of Cassandra looks like the following example.


# Update command of Cassandra
UPDATE student SET age=23,name='Alice'  
WHERE id=2;  

The command above updates the row where id is equal to 2.

Cassandra Vs MongoDB

But, in MongoDB, data is updated by assigning the fields to update and the new values. Updates to the specified document are made using the method described below.


# Update in MongoDB
db.student.updateOne({_id : 1},
 {$set: { name : "John"}})

Here, the output indicates that the document is updated with the new value.

8. DELETE in Cassandra Vs MongoDB

Finally, the deletion operation is handled by both databases with different syntaxes. Cassandra data is deleted by specifying the row key or a range of keys. The delete command is presented as follows.


# Delete command in Cassandra
DELETE FROM STUDENT WHERE id= 3;

Now, that row has been deleted from the table in the output.

Cassandra Vs MongoDB differences

On the other hand, MongoDB deletes data by specifying a query that matches the documents to be deleted. Here’s is an example of a MongoDB query to delete specific data.


# MongoDB Delete query
db.student.deleteOne({ _id: 3 })

The output here displayed the deletion of the following document.

Cassandra Vs MongoDB

9. Conclusion

In conclusion, the use of Cassandra and MongoDB is heavily influenced by the project’s unique requirements. Cassandra should be utilized if we would like to store and manage immense amounts of data across several data centers. While MongoDB is required for a flexible data model for a web or mobile application.

More details about this topic can be found here.