Spring to NestJS with MongoDB and Mongoose: Debit Vanished

30 September 2026 nestjstypescriptnodejsjavaspring-bootmongodb
PDF The design sheet The full architecture and the verified references. Free, no email.
Thumbnail for Spring to NestJS with MongoDB and Mongoose: Debit Vanished 4:08
Watch on YouTube, or read on below.
Run this yourself → Clone the repo, docker compose up, and watch it happen

One account with a balance of 100, and two requests at the same time: one debits 30, the other 50. The balance should be 20. It was 50, in Spring Data MongoDB and in Mongoose, and both saves reported success. Measured on 30 September 2026 on MongoDB 8.3.11, Mongoose 9.10.3 and Spring Boot 4.1.1.

Changing frameworks did not remove the race

Both requests loaded the account while it said 100, and each saved its own result. Mongoose documents carry a __v field, and it did not stop this. Two different tools fix it:

Validation belongs to the write path

With @Min(0) on the balance, Spring Data MongoDB saved -500 until ValidatingEntityCallback was registered; then the save was rejected. Mongoose rejected -500 on save(), stored it through updateOne(), and rejected it again with runValidators: true. A direct update went past the rule on both stacks.

Put the rule in the filter

Mongoose update validators do not run on $inc. With validators switched on, taking 500 from a balance of 100 left -400. Making the rule part of the match, updateOne({ _id, balance: { $gte: 500 } }, { $inc: { balance: -500 } }), matched nothing and kept the balance at 100.

The full code for every measurement is in the repository, and the free design sheet above has the checklist on one page.


Watch next

New videos land on Code with Sam - subscribe there if this was useful.

Get the interview pack

A free, practical pack for engineers preparing for senior backend interviews - the questions that actually get asked, and how to answer them like someone who has done the work.

New articles and videos when they land. No spam, and one click to leave.