getLastError
Definition
Changed in version 2.6: A new protocol for write operations integrates write concerns with thewrite operations, eliminating the need for a separate getLastError
.Most write methods now returnthe status of the write operation, including error information.In previous versions, clients typically used the getLastError
incombination with a write operation to verify that the writesucceeded.
Returns the error status of the preceding write operation on thecurrent connection.
getLastError
uses the following prototype form:
- { getLastError: 1 }
getLastError
uses the following fields:
FieldTypeDescriptionj
booleanIf true
, wait for the next journal commit before returning, ratherthan waiting for a full disk flush. If mongod
does not havejournaling enabled, this option has no effect. If this option isenabled for a write operation, mongod
will wait _no more_than 1/3 of the current commitIntervalMs
beforewriting data to the journal.w
integer or stringWhen running with replication, this is the number of servers toreplicate to before returning. A w
value of 1 indicates theprimary only. A w
value of 2 includes the primary and at leastone secondary, etc. In place of a number, you may also set w
tomajority
to indicate that the command should wait until thelatest write propagates to a majority of the voting replica setmembers.
Changed in version 3.0: In previous versions, majority
referred to the majority of allmembers of the replica set instead of the majority of the votingmembers.
If using w
, you should also use wtimeout
. Specifyinga value for w
without also providing a wtimeout
may causegetLastError
to block indefinitely.wtimeout
integerOptional. Milliseconds. Specify a value in milliseconds to control howlong to wait for write propagation to complete. If replication doesnot complete in the given timeframe, the getLastError
command will return with an error status.
See also
Output
Each getLastError()
command returns a document containing asubset of the fields listed below.
getLastError.
ok
ok
istrue
when thegetLastError
command completes successfully.
Note
A value of true
does not indicate that the precedingoperation did not produce an error.
getLastError.
err
err
isnull
unless an error occurs. Whenthere was an error with the preceding operation,err
containsa string identifying the error.
New in version 2.6.
errmsg
contains the description of the error.errmsg
only appears if there was an error withthe preceding operation.
getLastError.
code
code
reports the preceding operation’s errorcode. For description of the error, seeerr
anderrmsg
.
getLastError.
lastOp
- When issued against a replica set member and the precedingoperation was a write or update,
lastOp
is theoptime timestamp in the oplog of the change.
getLastError.
n
- If the preceding operation was an update or a remove operation, butnot a
findAndModify
operation,n
reports the number of documents matched bythe update or remove operation.
For a remove operation, the number of matched documents will equalthe number removed.
For an update operation, if the operation results in nochange to the document, such as setting the value of the field toits current value, the number of matched documents may be smallerthan the number of documents actually modified. If the updateincludes the upsert:true
option and results in the creation of anew document, n
returns the number ofdocuments inserted.
n
is 0
if reporting on an update or removethat occurs through a findAndModify
operation.
getLastError.
syncMillis
syncMillis
is the number of milliseconds spentwaiting for the write to disk operation (e.g. write to journalfiles).
getLastError.
shards
- When issued against a sharded cluster after a write operation,
shards
identifies the shards targeted in thewrite operation.shards
is present in theoutput only if the write operation targets multiple shards.
getLastError.
singleShard
- When issued against a sharded cluster after a write operation,identifies the shard targeted in the write operation.
singleShard
is only present if the writeoperation targets exactly one shard.
getLastError.
updatedExisting
updatedExisting
istrue
when an updateaffects at least one document and does not result in anupsert.
getLastError.
upserted
- If the update results in an insert,
upserted
is the value of_id
field of the document.
Changed in version 2.6: Earlier versions of MongoDB includedupserted
only if _id
was anObjectId.
getLastError.
wnote
- If set,
wnote
indicates that the preceding operation’s errorrelates to using thew
parameter togetLastError
.
See
Write Concern for more information aboutw
values.
getLastError.
wtimeout
wtimeout
istrue
if thegetLastError
timed out because of thewtimeout
setting togetLastError
.
getLastError.
waited
- If the preceding operation specified a timeout using the
wtimeout
setting togetLastError
, thenwaited
reports the number of millisecondsgetLastError
waited before timing out.
getLastError.
wtime
getLastError.wtime
is the number of milliseconds spentwaiting for the preceding operation to complete. IfgetLastError
timed out,wtime
andgetLastError.waited
are equal.
getLastError.
writtenTo
- If writing to a replica set,
writtenTo
is anarray that contains the hostname and port number of the members thatconfirmed the previous write operation, based on the value of thew
field in the command.
Examples
Confirm Replication to Two Replica Set Members
The following example ensures the preceding operation has replicated totwo members (the primary and one other member). The command alsospecifies a timeout of 5000
milliseconds to ensure thatthe:dbcommand:getLastError command does not block forever if MongoDBcannot satisfy the requested write concern:
- db.runCommand( { getLastError: 1, w: 2, wtimeout:5000 } )
Confirm Replication to a Majority of a Replica Set
The following example ensures the write operation has replicated to amajority of the voting members of the replica set. The command alsospecifies a timeout of 5000
milliseconds to ensure thatthe:dbcommand:getLastError command does not block forever if MongoDBcannot satisfy the requested write concern:
- db.runCommand( { getLastError: 1, w: "majority", wtimeout:5000 } )