This content originally appeared on DEV Community and was authored by krishna-Tiwari
Your complete attention motivates me to write further articles, that might help others too.
As of today's date 2024/8/11, I am currently working on an e-commerce project for my practice with MongoDB, node js, express and react. Initially, the project went as well as I thought and worked perfectly.
As we know every e-commerce platform must have some modules like products, sellers, customers and orders. I think these modules are minimal for every e-commerce platform.
Here I have all the product-related attributes on the product module and the orders module has the history of ordered items.
While maintaining the CRUD operation over the Product, a scenario triggered in my mind relating to the deletion of the product but what if the product was already ordered earlier and now have to delete that product? How could we maintain this scenario?
We have to maintain the historical data consistent but how could we fetch the product details if it has been deleted.
This is a real headache for me.
I assisted my respected teacher regarding this situation and have found some conclusions that might be suitable for this situation.
-
Soft Delete
Instead of physically deleting the product from the product collection, we can implement a soft delete. This involves adding a boolean key, like
2.is_deleted
, to the product collection. When a product isdeleted
, we have to set this flag totrue
instead of removing the document from the collection.
The product data is retained in the database, allowing us to reference it in historical order.
But we have a consideration while fetching products, keep in mind to only fetch productsis_deleted
isfalse
.Archiving
In this scenario, we have to maintain a separate collection for deleted product items. Instead of deleting, we could move the product data to an archive collection. This keeps the original product table clean while still retaining the historical data in a separate product_archive collection.
3.
Separation of active and archived data can improve performance and make it easier to manage large datasets.
However, we have a consideration that we have to maintain complex queries to fetch archived data.Details in order collection
Store the necessary product details directly in the orders collection, such as product_name, price, image_link etc., at the time of the order. This way, even if the product is deleted, the order still has all relevant data. This simplifies the structure, as
product data
is already present in order collection. But this technique causes data redundancy, Which is so common in MongoDB. 😁😁Flexibility of MongoDb no need to normalize the table.
Note: Not recommended
What I have implemented ?
I have implemented the first technique, soft delete because just a single
key solves all the problems for mine.
You can implement other techniques too based on your use case and what makes you feel better.
This content originally appeared on DEV Community and was authored by krishna-Tiwari
krishna-Tiwari | Sciencx (2024-08-11T15:39:26+00:00) Balancing Data Deletion and Historical Accuracy. Retrieved from https://www.scien.cx/2024/08/11/balancing-data-deletion-and-historical-accuracy/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.