QuickDevGuide

Home › MongoDB

Schema Validation & Transactions — MongoDB

Schema validation MongoDB is schema-flexible, not schema-less. A $jsonSchema validator rejects bad writes at the database level — useful when several services write to one collection. Option Values validationLevel strict (all writes) / moderate (only already-valid docs) validationAction error (reject) / warn (log only) Multi-document transactions Single-document writes are always atomic. For changes spanning documents or collections (e.g. move money between two accounts) use a transaction on a replica set or sharded cluster. Keep transactions short (default limit 60 s) and touch few documents. Use session.withTransaction() — it retries on transient errors. Prefer modelling so that one document holds what changes together; transactions are the exception, not the norm. Write & read…

Key Takeaways