MongoDB 支持 multi-document ACID transactions(v4.0 用于副本集,v4.2 用于分片集群)——将跨多个文档/集合的操作分组,使它们全部成功或全部失败。但是,MongoDB 的文档模型通常会通过原子单文档操作使事务 不必要。
单文档操作已经是原子的
KEY POINT: operations on a SINGLE document are ALWAYS atomic in MongoDB.
→ Because related data is often EMBEDDED in one document, many operations that would
need a transaction in SQL are a single atomic document update in MongoDB.
→ Good schema design (embedding) often eliminates the NEED for multi-doc transactions.
db..(
{ : id },
{ : { : - }, : { : { : - } } }
);
