How to ensure minimum value during both creation and deletion in mongodb?

Viewed 33

Consider:

  1. Summary schema with a minimum_score property
  2. PersonRecord schema with a score property
  3. A single Summary document should contain minimum_score of all the PersonRecord documents.

Following two actions are done in a transaction:

  1. Insertion of new document in PersonRecord collection
  2. Update Summary document's minimum_score property using { $min: personRecord.score }

However, there is another requirement to allow a person to delete his PersonRecord document. So consider the scenarios such as following:

  1. All of the persons delete their PersonRecord documents
  2. One person deletes his PersonRecord document while another person is inserting a new one.

How can we handle the scenarios in mongodb (mongoose) if we ALWAYS want to have the correct minimum_score value in Summary document?

I can think of achieve this is using the principle of eventual consistency. Is there any other way?

0 Answers
Related