db.getLastError()
Definition
db.
getLastError
(, ) - Specifies the level of write concern for confirming the successof previous write operation issued over the same connection andreturns the
error string
for that operation.
When using db.getLastError()
, clients must issue thedb.getLastError()
on the same connection as the writeoperation they wish to confirm.
Changed in version 2.6: A new protocol for write operations integrates write concerns with thewrite operations, eliminating the need for a separate db.getLastError()
.Most write methods now returnthe status of the write operation, including error information.In previous versions, clients typically used the db.getLastError()
incombination with a write operation to verify that the writesucceeded.
The db.getLastError()
can accept the following parameters:
ParameterTypeDescriptionw
int or stringOptional. The write concern’s w
value.wtimeout
intOptional. The time limit in milliseconds.
Behavior
The returned error string
provides error information on theprevious write operation.
If the db.getLastError()
method itself encounters an error, such as anincorrect write concern value, the db.getLastError()
throws an exception.
Example
The following example issues a db.getLastError()
operationthat verifies that the preceding write operation, issued over the sameconnection, has propagated to at least two members of the replica set.
- db.getLastError(2)
See also
getLastError
and Write Concern forall options, Write Concern for a conceptualoverview, MongoDB CRUD Operations for information about allwrite operations in MongoDB.