[Some background information - possibly skippable]
To begin with, I have barely any understanding of database management and just shallow experience with mongoose and node in the backend realm(a couple of udemy courses). Udemy courses made me believe that mongodb was still a viable choice for a database with relational properties and off I went working on a backend for a forum-like website. After learning about transactions, I attempted to implement them in my backend, since it seemed perfectly necessary to implement a rollback feature when executing an array of queries. However it turns out that transactions are only possible on replica sets - which also appeared to require a minimum of 2. 3 databases for a startup MVP was obviously considered an overkill.
[The question]
Is it possible to implement transactions with only 1 database? If so how?
If the above is not possible, how would one launch a mongodb database with minimum configuration while implementing transactions, with the fact that the database is for a startup MVP in consideration.
(For anyone who has experience in implementing a production-level mongo database in a similar scenario) If not using transactions was considered, how to send queries editing/creating multiple documents to a mongoDB database safely without transactions, while not spraying every bit of code with try-catches consisting of queries to rollback every point of failure(which I considered as way too much overhead)
I have a tight deadline, and have already done a substantial bit of groundwork and a couple of routes using mongoose, which means ditching mongodb for a relational database will be a difficult option at the moment.
I think I've googled everything related to the subject at matter and even tried blogs / articles in the second page of a google search(which many including myself consider as the dark web /s). Yet I do think I may have missed what I was looking for and an answer consisting of just links is also welcome.
Thank you for reading!
You need a replica set[*] to use transactions, but you can create a single-node replica set for testing purposes.
The full procedure is described in documentation, for a single-node RS you follow that as written but only configure a single member.
Briefly, you need to pass --replSet
argument to mongod
and then connect to it through mongo
shell and run rs.initiate()
.
Note that transactions aren't a magic solution to all problems. There are scenarios where they are appropriate and scenarios where MongoDB provides other functionality that would be a better fit.
[*] or a sharded cluster with MongoDB 4.2+, but this involves more setup work.