DEV Community

Cover image for Introducing PolygonDB: A Modern and Flexible Alternative to MongoDB with Advanced Data Management Features
Yosi Leyid for Hasidic Development Group

Posted on

Introducing PolygonDB: A Modern and Flexible Alternative to MongoDB with Advanced Data Management Features

PolygonDB is an open-source database management system that is designed to be a modern alternative to MongoDB. It's built in Rust, a programming language known for its speed and reliability, and it boasts a number of features that make it a compelling choice for developers who need to store and manage data.

One of the key features of PolygonDB is its support for complex data types. Let's say, for example, that you're building an e-commerce site and you need to store information about each product. In MongoDB, you might create a collection for products and store each product as a document, like this:

{
  "_id": ObjectId("612f4a4d9a4ce4d4a3a0e3fe"),
  "name": "iPhone 13 Pro",
  "description": "The latest and greatest iPhone",
  "price": 999.99,
  "color": "Graphite",
  "storage": "128GB"
}
Enter fullscreen mode Exit fullscreen mode

This works fine, but what if you need to store additional information about each product, such as customer reviews or a list of related products? In MongoDB, you would have to store this information as nested documents or arrays, which can get messy and difficult to work with.

With PolygonDB, you can store each product as a YAML or TOML file, like this:

name = "iPhone 13 Pro"
description = "The latest and greatest iPhone"
price = 999.99
color = "Graphite"
storage = "128GB"
reviews = ["612f4ad69a4ce4d4a3a0e4ff", "612f4ae69a4ce4d4a3a0e54c"]
related_products = ["612f4b099a4ce4d4a3a0e5c4", "612f4b209a4ce4d4a3a0e60f"]
Enter fullscreen mode Exit fullscreen mode

This makes it much easier to work with complex data types, since you can use familiar file-based operations to read, write, and manipulate data.

Another key feature of PolygonDB is its support for transactions. Let's say, for example, that you're building a banking app and you need to ensure that transfers between accounts are atomic and consistent. With PolygonDB, you can use transactions to ensure that all changes are committed or rolled back together, so that you don't end up with inconsistent or corrupted data.

Here's an example of how you might use transactions in PolygonDB:

let mut txn = db.transaction().unwrap();

let account_a = txn.get_document("accounts", "account_123").unwrap();
let account_b = txn.get_document("accounts", "account_456").unwrap();

if account_a["balance"] >= transfer_amount {
    let new_balance_a = account_a["balance"] - transfer_amount;
    let new_balance_b = account_b["balance"] + transfer_amount;

    txn.update_document("accounts", "account_123", |doc| {
        doc["balance"] = new_balance_a;
    });

    txn.update_document("accounts", "account_456", |doc| {
        doc["balance"] = new_balance_b;
    });

    txn.commit().unwrap();
} else {
    txn.rollback().unwrap();
}
Enter fullscreen mode Exit fullscreen mode

This code retrieves two documents from the "accounts" collection, checks that there is enough money in one account to make a transfer, and then updates both documents atomically. If there is not enough money, the transaction is rolled back.

Finally, PolygonDB has built-in support for indexing, which makes it easy to perform complex queries and sort data. You can create indexes on one or more fields, and PolygonDB will automatically maintain the index as you add, remove, or modify documents. This can greatly improve the performance of your queries, especially for large collections.

Here's an example of how you might create an index in PolygonDB:

db.create_index("products", "name").unwrap();
Enter fullscreen mode Exit fullscreen mode

This code creates an index on the "name" field in the "products" collection. Now, when you perform a query that includes the "name" field, PolygonDB will use the index to quickly find matching documents.

In summary, PolygonDB is a modern and flexible database management system that offers a number of features that make it a compelling alternative to MongoDB. Its support for complex data types, transactions, and indexing make it a powerful tool for developers who need to store and manage data. If you're interested in learning more, I encourage you to check out the PolygonDB code base and documentation on GitHub.

Top comments (0)