- Reference >
- Database Commands >
- getLastError
getLastError¶
-
getLastError
¶ The
getLastError
command returns the error status of the last operation on the current connection. By default MongoDB does not provide a response to confirm the success or failure of a write operation, clients typically usegetLastError
in combination with write operations to ensure that the write succeeds.Consider the following prototype form.
The following options are available:
Parameters: - j (boolean) – If
true
, wait for the next journal commit before returning, rather than a full disk flush. Ifmongod
does not have journaling enabled, this option has no effect. - w – When running with replication, this is the number of
servers to replicate to before returning. A
w
value of 1 indicates the primary only. Aw
value of 2 includes the primary and at least one secondary, etc. In place of a number, you may also setw
tomajority
to indicate that the command should wait until the latest write propagates to a majority of replica set members. If usingw
, you should also usewtimeout
. Specifying a value forw
without also providing awtimeout
may causegetLastError
to block indefinitely. - fsync (boolean) – If
true
, wait formongod
to write this data to disk before returning. Defaults to false. In most cases, use thej
option to ensure durability and consistency of the data set. - wtimeout (integer) – Optional. Milliseconds. Specify a value in
milliseconds to control how long to
wait for write propagation to complete. If
replication does not complete in the given
timeframe, the
getLastError
command will return with an error status.
See also
Write Concern, Replica Set Write Concern, and
db.getLastError()
.- j (boolean) – If