mongodb 锁 :https://docs.mongodb.com/manual/faq/concurrency/
Operation | Lock Type |
---|---|
Issue a query | Read lock |
Get more data from a cursor | Read lock |
Insert data | Write lock |
Remove data | Write lock |
Update data | Write lock |
Map-reduce | Read lock and write lock, unless operations are specified as non-atomic. Portions of map-reduce jobs can run concurrently. |
Create an index | Building an index in the foreground, which is the default, locks the database for extended periods of time. |
Deprecated since version 3.0. | Write lock. The db.eval() method takes a global write lock while evaluating the JavaScript function. To avoid taking this global write lock, you can use the eval command withnolock: true. |
Deprecated since version 3.0. | Write lock. By default, eval command takes a global write lock while evaluating the JavaScript function. If used withnolock: true, the eval command does not take a global write lock while evaluating the JavaScript function. However, the logic within the JavaScript function may take write locks for write operations. |
aggregate() | Read lock |
当后台模式启动时,其他的操作,包含写,在创建索引期间不会被阻塞。该索引在创建完成前不会被应用到查询中去。
之前我司就是发现数据库数据量大了之后怕出现慢查询就给字段创建索引,结果一个同事把background打成backgound,结果mongo并没有报错。然后就没在后台跑,导致一段时间大概是五分钟没办法访问,排查很久之后发现这个脚本问题改过来。
尽管该操作是“后台”类型意味着其他操作可以并发执行,但是该命令在创建完成前不会立即返回到shell提示符。要同时执行其他操作,可以另外打开一个mongo shell实例。
需要注意的是,后台模式创建索引是新增加的方法,它比默认的前端模式要慢:需要更多的时间来创建索引。