能不能把数据暴力的刷到硬盘上,当然是可以的,mongodb给我们提供了fsync+lock机制就能满足我们提的需求。

fsync+lock首先会把缓冲区数据暴力刷入硬盘,然后给数据库一个写入锁,其他实例的写入操作全部被阻塞,直到fsync

+lock释放锁为止。

这里就不测试了。

加锁:    db.runCommand({"fsync":1,"lock":1})

释放锁: db.$cmd.unlock.findOne()

fsync

Forces the mongod process to flush all pending writes from the storage layer to disk. Optionally, you can use fsync to lock the mongod instance and block write operations for the purpose of capturing backups.

As applications write data, MongoDB records the data in the storage layer and then writes the data to disk within the syncPeriodSecs interval, which is 60 seconds by default. Run fsync when you want to flush writes to disk ahead of that interval.

The fsync command has the following syntax:

{ fsync: 1, async: <Boolean>, lock: <Boolean> }

The fsync command has the following fields:

Field Type Description
fsync integer Enter “1” to apply fsync.
async boolean Optional. Runs fsync asynchronously. By default, the fsync operation is synchronous.
lock boolean Optional. Locks mongod instance and blocks all write operations.

Examples

Run Asynchronously

The fsync operation is synchronous by default. To run fsync asynchronously, use the async field set totrue:

{ fsync: 1, async: true }

The operation returns immediately. To view the status of the fsync operation, check the output ofdb.currentOp().

Lock mongod Instance

NOTE

Changed in version 3.2: fsync command with the lock option can ensure that the data files do not change for MongoDB instances using either the MMAPv1 or the WiredTiger storage engines, thus providing consistency for the purposes of creating backups.

In previous MongoDB versions, fsync command with the lock option cannot guarantee a consistent set of files for low-level backups (e.g. via file copy cpscptar) for WiredTiger.

The primary use of fsync is to lock the mongod instance in order to back up the files within mongod‘sdbPath. The operation flushes all data to the storage layer and blocks all write operations until you unlock themongod instance.

To lock the database, use the lock field set to true:

{ fsync: 1, lock: true }

You may continue to perform read operations on a mongod instance that has a fsync lock. However, after the first write operation all subsequent read operations wait until you unlock the mongod instance.

Unlock mongod Instance

To unlock the mongod, use db.fsyncUnlock():

db.fsyncUnlock();

见:https://docs.mongodb.com/manual/reference/command/fsync/

最新文章

  1. Yii2的深入学习--别名(Aliases)
  2. POJ 1012 Joseph
  3. NSQ:分布式的实时消息平台
  4. [Redux] Fetching Data on Route Change
  5. MVC3.0 提交表单的方法
  6. python查看模块及相关函数帮助
  7. Android调用系统的Activity、ContentProvider、Service、Broadcast Receiver
  8. 有关JAVA基础学习中的集合讨论
  9. C#导出EXCEL的方法
  10. .NET插件技术-应用程序热升级
  11. Python ——报错集锦
  12. gitIgnore说明
  13. mysql数据的基本操作
  14. java反射对实体类取值和赋值
  15. nGrinder TestGroovy.groovy
  16. ASP.NET MVC 4 (十二) Web API
  17. Neo4j 文档
  18. Spring boot整合Hive
  19. python openpyxl.md
  20. 巧用PHP双$功能兼容线上线下配置文件

热门文章

  1. javascript --- 继承小结
  2. Windows Server 2003中报PerfDisk “无法从系统读取磁盘性能信息。
  3. 浅谈拒绝服务攻击的原理与防御(4):新型DDOS攻击 – Websocket和临时透镜
  4. 转置卷积&amp;&amp;膨胀卷积
  5. Unity3D:Gizmos画圆(原创)
  6. 数据结构与算法系列----AC自己主动机
  7. C语言关键字—-sizeof 、typedef、const、static、register、extern、#define
  8. 算法NB三人组
  9. 机器学习资源汇总----来自于tensorflow中文社区
  10. Vue.directive 自定义指令