I am building a web application with MongoDB. Now I am creating administration pages that allow administrators to add or remove items on a website. A bulk import feature will be added to the page, allowing you to import content from local CSV files. The problem is how to implement this feature.
The easiest way is to convert the downloaded CSV files to JSON and simply insert them using the operator db.items.insert([{...}, {...}, ...]).
If nullreturned db.getLastError(), the import is successful. No problems.
However, what should be done if an error occurs during the bulk insert? Since there is no transaction, inserted items cannot be undone. Therefore, a repeated attempt to insert will lead to duplication of documents.
What is the best way to solve this problem?
source
share