Definition
getLastErrorChanged in version 2.6: A new protocol for write operations integrates write concerns with the write operations, eliminating the need for a separate
getLastError. Most write methods now return the status of the write operation, including error information. In previous versions, clients typically used thegetLastErrorin combination with a write operation to verify that the write succeeded.Returns the error status of the preceding write operation on the current connection.
getLastErroruses the following prototype form:{ getLastError: 1 } getLastErroruses the following fields:FieldTypeDescriptionjboolean
If
true, wait for the next journal commit before returning, rather than waiting for a full disk flush. Ifmongoddoes not have journaling enabled, this option has no effect. If this option is enabled for a write operation,mongodwill wait no more than 1/3 of the currentcommitIntervalMsbefore writing data to the journal.winteger or string
When running with replication, this is the number of servers to replicate to before returning. A
wvalue of 1 indicates the primary only. Awvalue of 2 includes the primary and at least one secondary, etc. In place of a number, you may also setwtomajorityto indicate that the command should wait until the latest write propagates to a majority of the voting replica set members.If using
w, you should also usewtimeout. Specifying a value forwwithout also providing awtimeoutmay causegetLastErrorto block indefinitely.wtimeoutinteger
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
getLastErrorcommand will return with an error status.commentany
Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:
mongod log messages, in the
attr.command.cursor.commentfield.Database profiler output, in the
command.commentfield.currentOpoutput, in thecommand.commentfield.
A comment can be any valid BSON type (string, integer, object, array, etc).
New in version 4.4.
Output
Each getLastError command returns a document containing a
subset of the fields listed below.
getLastError.okokistruewhen thegetLastErrorcommand completes successfully.Note
A value of
truedoes not indicate that the preceding operation did not produce an error.
getLastError.errerrisnullunless an error occurs. When there was an error with the preceding operation,errcontains a string identifying the error.
getLastError.nsIf the error is a duplicate key error, returns the namespace of the collection.
New in version 4.4.
getLastError.indexIf the error is a duplicate key error, returns the index whose unique key constraint is violated.
New in version 4.4.
getLastError.errmsgerrmsgcontains the description of the error.errmsgonly appears if there was an error with the preceding operation.
getLastError.codecodereports the preceding operation's error code. For description of the error, seeerranderrmsg.
getLastError.lastOpWhen issued against a replica set member and the preceding operation was a write or update,
lastOpis the optime timestamp in the oplog of the change.
getLastError.nIf the preceding operation was an update or a remove operation, but not a
findAndModifyoperation,nreports the number of documents matched by the update or remove operation.For a remove operation, the number of matched documents will equal the number removed.
For an update operation, if the operation results in no change to the document, such as setting the value of the field to its current value, the number of matched documents may be smaller than the number of documents actually modified. If the update includes the
upsert:trueoption and results in the creation of a new document,nreturns the number of documents inserted.nis0if reporting on an update or remove that occurs through afindAndModifyoperation.
getLastError.syncMillissyncMillisis the number of milliseconds spent waiting for the write to disk operation (e.g. write to journal files).
getLastError.shardsWhen issued against a sharded cluster after a write operation,
shardsidentifies the shards targeted in the write operation.shardsis present in the output only if the write operation targets multiple shards.
getLastError.singleShardWhen issued against a sharded cluster after a write operation, identifies the shard targeted in the write operation.
singleShardis only present if the write operation targets exactly one shard.
getLastError.updatedExistingupdatedExistingistruewhen an update affects at least one document and does not result in an upsert.
getLastError.upsertedIf the update results in an insert,
upsertedis the value of_idfield of the document.
getLastError.wnoteIf set,
wnoteindicates that the preceding operation's error relates to using thewparameter togetLastError.
getLastError.wtimeoutwtimeoutistrueif thegetLastErrortimed out because of thewtimeoutsetting togetLastError.
getLastError.waitedIf the preceding operation specified a timeout using the
wtimeoutsetting togetLastError, thenwaitedreports the number of millisecondsgetLastErrorwaited before timing out.
getLastError.wtimegetLastError.wtimeis the number of milliseconds spent waiting for the preceding operation to complete. IfgetLastErrortimed out,wtimeandwaitedare equal.
getLastError.writtenToIf writing to a replica set,
writtenTois an array that contains the hostname and port number of the members that confirmed the previous write operation, based on the value of thewfield in the command.
Examples
Confirm Replication to Two Replica Set Members
The following example ensures the preceding operation has replicated to
two members (the primary and one other member). The command also
specifies a timeout of 5000 milliseconds to ensure that
the getLastError command does not block forever if MongoDB
cannot 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 a
majority of the voting members of the replica set. The command also
specifies a timeout of 5000 milliseconds to ensure that
the getLastError command does not block forever if MongoDB
cannot satisfy the requested write concern:
db.runCommand( { getLastError: 1, w: "majority", wtimeout:5000 } )