0

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:

Books(Collection)

_id Name isReviewed [Boolean]

Reviews (Collection)

_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

isherwood
  • 58,414
  • 16
  • 114
  • 157
Vsoma
  • 595
  • 1
  • 5
  • 17
  • I think your only viable option (where operation is staying atomic) is nesting review documents as sub-documents of books. – bardzusny Jun 06 '15 at 10:04
  • @bardzusny Thank you for the response. I considered that option before I came up with this design of having two collections. As I mentioned in the question, I needed two collections as "Reviews" will keep growing and are mutable. – Vsoma Jun 08 '15 at 13:52

1 Answers1

0

since mongodb provides AUTOMICITY at document level, You can pre-Join your books and reviews collection as below :

db.Books.insert(
{
 "_id" : 1,
 "Name": "ABC",
 "reviews": [ ],
 "count" : 0

}

P.S : your document size should be within 16MB size at MAX. if you are expecting your "reviews" will be more than that, you will have to split into two collection and handle the case in your code.

Yathish Manjunath
  • 1,919
  • 1
  • 13
  • 23
  • Thank you for the response. I understand that, in fact, my case is where "Reviews" array will keep growing and should be able to perform CRUD operations on the array items. (As I mentioned in my question) . When its a case of two collections, you mentioned I need to handle in my code. Can you please give me an example code of that? I'm all for doing that if it helps me keep my data consistent. I appreciate your help – Vsoma Jun 08 '15 at 13:56