DEV Community

Caroline
Caroline

Posted on

MongoDB vs DynamoDB: Differences between them

Context

When I was studying DynamoDB, I had some difficulties understanding its modeling and use cases due to my background in relational databases. I couldn't find much content that compared the differences between DynamoDB and MongoDB. As MongoDB is the only NoSQL database I'm familiar with, I am bringing benchmarks and use cases. DynamoDB and MongoDB are two NoSQL databases, and there are some key differences between them that make the data structure and use case better for each in specific situations.

Use Case

DynamoDB:

Is know for his real-time data system, making himself worth for applications that require high read capacity.
example: social media, and data analysis.

MongoDB:

Is a good choice for applications that require more flexibility in data schema and complex queries, a business rule that is good example: e-commerce or any project with lots of change in business rules

Schema Modeling:

Assuming that I have a generic store business rule, the modeling for each database would be:

MongoDB Schema:

db.createCollection("products")
{
  "name": "string",
  "description": "string",
  "price": "number",
}

db.createCollection("order")
{
  "id": "string",
  "datetime": "string",
  "itens": [
    {
      "productName": "string",
      "quantity": "number",
      "price": "number"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

DynamoDB Schema:

{
  "products": {
    "name": "string",
    "description": "string",
    "price": "number",
  },
  "order": {
    "id": "string",
    "datetime": "string",
    "itens": [
      {
        "productName": "string",
        "quantity": "number",
        "price": "number"
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

As we can see, in Mongo, we have two collections (or tables), whereas in DynamoDB, we only had one table with different types of data and business contexts. This approach of using a single table is called a 'wide table,' and it allows companies to store multiple types of data items with different sets of attributes within the same table. This approach can help simplify application logic and reduce data management complexity compared to multi-table modeling.
Here's a recommended article about the art of using just one table: https://www.alexdebrie.com/posts/dynamodb-single-table/

Indexes

Definition

Simply put, an index is a pointer to data in a table. An index in a database is very similar to an index in the back of a book.
They are a powerful tool used in the background of a database to speed up querying, they power queries by providing a method to quickly lookup the requested data.

It's important to note that creating indexes can have an impact on write performance and storage requirements, so it's important to carefully choose which indexes to create based on the queries that are being performed most frequently

Note: Indexes can affect the cost of since they require additional read and write capacity units.

Indexes on DynamoDB:

Can offer feature such as:

Global secondary indexes

Is a type of index containing a partition key and a sort key different from the base table's primary key. It is known as the "global" secondary index since the queries on the index can access data from multiple partitions of the base table.

Local secondary indexes

Those indexes allow developers to create flexible access to data in the wide table, in simple lines: perform queries that don't match the primary key of the table without impacting performance and scalability

MongoDB:

Single Field Indexes:

A single field index only includes data from a single field of the documents in a collection.

Compound Indexes

A compound index includes more than one field of the documents in a collection.

Multikey Indexes:

A multikey index is an index on an array field, adding an index key for each value in the array.

Geospatial Indexes and Queries:

Support location-based searches on data that is stored as either GeoJSON objects or legacy coordinate pairs.

Text Indexes:

support search of string content in documents.

Hashed Index:

Maintain entries with hashes of the values of the indexed field and are primarily used with shard clusters to support hashed shard keys.

MongoDB can have multiples indexes, used to improve the performance of database queries by reducing the number of documents.

sources: (https://codecademy.com/article/sql-indexes; https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LSI.html; https://www.mongodb.com/docs/)

Top comments (0)