- $isolate操作符可以对多个文档的修改提供隔离性。
针对其他线程的并发写操作,$isolate保证了提交前其他线程无法修改对应的文档。
针对其他线程的读操作,$isolate保证了其他线程读取不到未提交的数据。
官网解释:
Prevents a write operation that affects multiple documents
from yielding to other reads or writes once the first document is written.
By using the $isolated option, you can ensure that
no client sees the changes until the operation completes or errors out.
- 但是$isolate有验证的性能问题,因为这种情况下线程持有锁的时间较长,严重的影响mongo的并发性。
- 注意:
The $isolated isolation operator
does not provide “all-or-nothing” atomicity for write operations.
【另外,$isolate也无法保证多个文档修改的一致性(all-or-nothing),
$isolate失败是可能只修改了部分文档。】
$isolated 没有事件回滚机制,即时在操作过程中报错,已经修改的不会出现回滚.
$isolated does not work with sharded(分片) clusters.
- 例如:
db.user.update(
{ _id : 1.0 , $isolated : 1 },
{ $inc : { age : 2 } },
{ multi: true }
)
Without the $isolated operator,
the multi-update operation will allow other operations
to interleave with its update of the matched documents.
$atomic:Deprecated since version 2.2: The $isolated operator replaces $atomic.
参考来源:https://docs.mongodb.com/manual/reference/operator/update/isolated/