Docs Home → Develop Applications → MongoDB Manual
db.fsyncLock()![](/docs/v5.2/assets/link.svg)
On this page
Definition![](/docs/v5.2/assets/link.svg)
db.fsyncLock()
Forces the
mongod
to flush all pending write operations to disk and locks the entiremongod
instance to prevent additional writes until the user releases the lock with a correspondingdb.fsyncUnlock()
command.Important
The
db.fsyncLock()
anddb.fsyncUnlock()
operations maintain a lock count.db.fsyncLock()
increments the lock count, anddb.fsyncUnlock()
decrements the lock count.To unlock a
mongod
instance for writes, the lock count must be zero. That is, for a given number ofdb.fsyncLock()
operations, you must issue a corresponding number ofdb.fsyncUnlock()
operations to unlock the instance for writes.db.fsyncLock()
has the syntax:db.fsyncLock() The operation returns a document with the following fields:
info
- Information on the status of the operationlockCount
(New in version 3.4)- The number of locks currently on the instance.seeAlso
- Link to thefsync
command documentation.ok
- The status code.
This command provides a simple wrapper around a
fsync
database command with the following syntax:{ fsync: 1, lock: true } db.fsyncLock()
is an administrative command. You can use this operation to locks the database and create a window for backup operations.
Behavior![](/docs/v5.2/assets/link.svg)
db.fsyncLock()
ensures that the data files are safe to copy
using low-level backup utilities such as cp
, scp
, or
tar
. A mongod
started using the copied
files contains user-written data that is indistinguishable from the
user-written data on the locked mongod
.
The data files of a locked mongod
may change due to
operations such as journaling syncs or
WiredTiger snapshots. While
this has no affect on the logical data (e.g. data accessed by
clients), some backup utilities may detect these changes and emit
warnings or fail with errors. For more information on MongoDB-
recommended backup utilities and procedures, see
MongoDB Backup Methods.
Example![](/docs/v5.2/assets/link.svg)
The following operation runs db.fsyncLock()
:
db.fsyncLock()
The operation returns the following status document that includes the
lockCount
:
{ "info" : "now locked against writes, use db.fsyncUnlock() to unlock", "lockCount" : NumberLong(1), "seeAlso" : "http://dochub.mongodb.org/core/fsynccommand", "ok" : 1 }
If you run db.fsyncLock()
again, the operation increments the
lockCount
:
{ "info" : "now locked against writes, use db.fsyncUnlock() to unlock", "lockCount" : NumberLong(2), "seeAlso" : "http://dochub.mongodb.org/core/fsynccommand", "ok" : 1 }
To unlock the instance for writes, you must run
db.fsyncUnlock()
twice to reduce the lockCount
to 0.