- Reference >
- Database Commands >
- Query and Write Operation Commands >
- getLastError
getLastError¶
On this page
Definition¶
-
getLastError
¶ Changed 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 thegetLastError
in combination with a write operation to verify that the write succeeded.Returns the error status of the preceding write operation on the current connection.
getLastError
uses the following prototype form:{ getLastError: 1 }
getLastError
uses the following fields:Field Type Description j
boolean If true
, wait for the next journal commit before returning, rather than waiting for a full disk flush. Ifmongod
does not have journaling enabled, this option has no effect. If this option is enabled for a write operation,mongod
will wait no more than 1/3 of the currentcommitIntervalMs
before writing data to the journal.w
integer or string When running with replication, this is the number of servers to replicate to before returning. A
w
value of 1 indicates the primary only. Aw
value of 2 includes the primary and at least one secondary, etc. In place of a number, you may also setw
tomajority
to indicate that the command should wait until the latest write propagates to a majority of the voting replica set members.Changed in version 3.0: In previous versions,
majority
referred to the majority of all members of the replica set instead of the majority of the voting members.If using
w
, you should also usewtimeout
. Specifying a value forw
without also providing awtimeout
may causegetLastError
to block indefinitely.wtimeout
integer 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 getLastError
command will return with an error status.See also
Output¶
Each getLastError()
command returns a document containing a
subset of the fields listed below.
-
getLastError.
ok
¶ ok
istrue
when thegetLastError
command completes successfully.Note
A value of
true
does not indicate that the preceding operation did not produce an error.
-
getLastError.
err
¶ err
isnull
unless an error occurs. When there was an error with the preceding operation,err
contains a string identifying the error.
-
getLastError.
errmsg
¶ New in version 2.6.
errmsg
contains the description of the error.errmsg
only appears if there was an error with the preceding operation.
-
getLastError.
code
¶ code
reports the preceding operation’s error code. For description of the error, seeerr
anderrmsg
.
-
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,
lastOp
is the optime timestamp in the oplog of the change.
-
getLastError.
n
¶ If the preceding operation was an update or a remove operation, but not a
findAndModify
operation,n
reports 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:true
option and results in the creation of a new document,n
returns the number of documents inserted.n
is0
if reporting on an update or remove that occurs through afindAndModify
operation.
-
getLastError.
syncMillis
¶ syncMillis
is the number of milliseconds spent waiting for the write to disk operation (e.g. write to journal files).
-
getLastError.
shards
¶ When issued against a sharded cluster after a write operation,
shards
identifies the shards targeted in the write operation.shards
is 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.
singleShard
is only present if the write operation targets exactly one shard.
-
getLastError.
updatedExisting
¶ updatedExisting
istrue
when an update affects at least one document and does not result in an upsert.
-
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 included
upserted
only if_id
was an ObjectId.
-
getLastError.
wnote
¶ If set,
wnote
indicates that the preceding operation’s error relates to using thew
parameter togetLastError
.See
Write Concern for more information about
w
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 spent waiting for the preceding operation to complete. IfgetLastError
timed out,wtime
andgetLastError.waited
are equal.
-
getLastError.
writtenTo
¶ If writing to a replica set,
writtenTo
is an array that contains the hostname and port number of the members that confirmed 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 to
two members (the primary and one other member). The command also
specifies a timeout of 5000
milliseconds to ensure that
the:dbcommand: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:dbcommand:getLastError command does not block forever if MongoDB
cannot satisfy the requested write concern:
db.runCommand( { getLastError: 1, w: "majority", wtimeout:5000 } )
Changed in version 2.6: In Master/Slave deployments,
MongoDB treats w: "majority"
as equivalent to
w: 1
. In earlier versions of MongoDB, w: "majority"
produces an
error in master/slave deployments.