Is there a possibility in Mongodb to update a document, based on a query for 'count' of docs in other collection? I'm looking for options to make it an atomic operation.
To be more specific, the following is how the two collections are designed:
_id Name isReviewed [Boolean]
_id bookID [_id in Books Collection] Comments
I came up with this design as "Reviews" array for a book will keep growing and are mutable. Now, there's a requirement to set isReviewed flag in "Book" document to "true" when there's a "Review" doc. created for respective Book. The flag will stay "true" as long as there's at least one associated review existing for a book.
The same flag will be set to "false", when there're no reviews existing for a book. (The default value for the flag is "false" when the Book doc. is created) When a Review is deleted, count of reviews for a book is calculated to see if it can be set to "false". (if count is 0, then set to false).
This system is designed for a multi-user environment sharing all resources such as books and reviews. All Users are given read/write permissions on all resources.(I know it sounds weird to allow all users to be able to create/ read/ edit/ delete all reviews, But, Let's say that's the case functionally).
Now, considering the above case, how can I ensure I perform an update related to setting the "isReviewed" flag based on the 'count' in the 'Reviews' collection?
Is this a case which can't be solved without transactions (I mean, do I need RDBMS)? I'm open to redesign my collections as well.
Any help is appreciated, Thank you