tyny.dev - the multifarious json builder | Product Hunt

Infinite loops with MongoDB transactions

Welcome to our blog series "A Developer's Notes". In this series we write down anything peculiar related to web development we came across, and couldn't find anywhere else.


MongoDB transactions initiated by session.startTransaction() or session.withTransaction() fail independent of the result of the promise they return.

In case of session.withTransaction() and an error thrown by the function within, the function is retried infinitely (might not be like that in all cases though)!

Technical environment

  • Node.js v10.24.1

  • npm mongodb v3.7.3 (official node.js driver)

  • MongoDB: v6.0.0


Don't let MongoDB operations within the transaction fail with a conscious error like E11000 (duplicate key error) for example.

This behaviour might come from the composition of versions we're using and can be distinctive to us. We're not saying that this is a bug or anything close to a general issue. It might be though. We're just recording our finding.

Happy coding!


This product has been added to your cart