- Reference >
- Database Commands >
- Query and Write Operation Commands >
- getLastError
getLastError¶
On this page
Definition¶
-
getLastError¶ Returns the error status of the preceding operation on the current connection. Clients typically use
.getLastErrorin combination with write operations to ensure that the write succeeds.getLastErroruses the following prototype form:getLastErroruses the following fields:Field Type Description jBoolean 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 currentjournalCommitIntervalbefore 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 replica set members. If usingw, you should also usewtimeout. Specifying a value forwwithout also providing awtimeoutmay causegetLastErrorto block indefinitely.fsyncBoolean If true, wait formongodto write this data to disk before returning. Defaults to false. In most cases, use thejoption to ensure durability and consistency of the data set.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.See also
Write Concern, Write Concern Reference, and Replica Acknowledged.
Output¶
Each getLastError() command returns a document containing a
subset of the fields listed below.
-
getLastError.ok¶ okistruewhen thegetLastErrorcommand completes successfully.Note
A value of
truedoes not indicate that the preceding operation did not produce an error.
-
getLastError.err¶ errisnullunless an error occurs. When there was an error with the preceding operation,errcontains a textual description of the error.
-
getLastError.connectionId¶ The identifier of the connection.
-
getLastError.lastOp¶ When 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.n¶ nreports the number of documents updated or removed, if the preceding operation was an update or remove operation.
-
getLastError.shards¶ When 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.singleShard¶ When 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.updatedExisting¶ updatedExistingistruewhen an update affects at least one document and does not result in an upsert.
-
getLastError.upserted¶ upsertedis an ObjectId that corresponds to the upserted document if the update resulted in an insert.upsertedis only present in the output ofgetLastErrorif the update statement did not include an_idfield.
-
getLastError.wnote¶ If set,
wnoteindicates that the preceding operation’s error relates to using thewparameter togetLastError.See
Write Concern Reference for more information about
wvalues.
-
getLastError.wtimeout¶ wtimeoutistrueif thegetLastErrortimed out because of thewtimeoutsetting togetLastError.
-
getLastError.waited¶ If the preceding operation specified a timeout using the
wtimeoutsetting togetLastError, thenwaitedreports the number of millisecondsgetLastErrorwaited before timing out.
-
getLastError.writtenTo¶ Array that lists the replica set members that have acknowledged the write operation.
-
getLastError.wtime¶ wtimeis the number of milliseconds spent waiting for the preceding operation to complete. IfgetLastErrortimed out,wtimeandgetLastError.waitedare equal.
Examples¶
Confirm Replication to Two Replica Set Members¶
The following example ensures the operation has replicated to two members (the primary and one other member):
Confirm Replication to a Majority of a Replica Set¶
The following example ensures the write operation has replicated to a majority of the configured members of the set.
Set a Timeout for a getLastError Response¶
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:
When wtimeout is 0, the getLastError operation
will never time out.