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 use getLastError in combination with write operations to ensure that the write succeeds.
Consider the following prototype form.
{ getLastError: 1 }
The following options are available:
| Parameters: |
|
|---|
See also
The following example ensures the operation has replicated to two members (the primary and one other member):
db.runCommand( { getLastError: 1, w: 2 } )
The following example ensures the write operation has replicated to a majority of the configured members of the set.
db.runCommand( { getLastError: 1, w: "majority" } )
Unless you specify a timeout, a getLastError command may block forever if MongoDB cannot satisfy the requested write concern. To specify a timeout of 5000 milliseconds, use an invocation that resembles the following:
db.runCommand( { getLastError: 1, w: 2, wtimeout:5000 } )