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 the getLastError 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. If mongod 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 current commitIntervalMs 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. A w value of 2 includes the primary and at least one secondary, etc. In place of a number, you may also set w to majority 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.

If using w, you should also use wtimeout. Specifying a value for w without also providing a wtimeout may cause getLastError to block indefinitely.

fsync Boolean If true, wait for mongod to write this data to disk before returning. Defaults to false. In most cases, use the j option to ensure durability and consistency of the data set.
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.


Each getLastError() command returns a document containing a subset of the fields listed below.


ok is true when the getLastError command completes successfully.


A value of true does not indicate that the preceding operation did not produce an error.


err is null unless an error occurs. When there was an error with the preceding operation, err contains a string identifying the error.


New in version 2.6.

errmsg contains the description of the error. errmsg only appears if there was an error with the preceding operation.


code reports the preceding operation’s error code. For description of the error, see err and errmsg.


The identifier of the connection.


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.


n reports the number of documents updated or removed, if the preceding operation was an update or remove operation.


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.


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.


updatedExisting is true when an update affects at least one document and does not result in an upsert.


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.


If set, wnote indicates that the preceding operation’s error relates to using the w parameter to getLastError.


Write Concern Reference for more information about w values.


wtimeout is true if the getLastError timed out because of the wtimeout setting to getLastError.


If the preceding operation specified a timeout using the wtimeout setting to getLastError, then waited reports the number of milliseconds getLastError waited before timing out.


getLastError.wtime is the number of milliseconds spent waiting for the preceding operation to complete. If getLastError timed out, wtime and getLastError.waited are equal.


Confirm Replication to Two Replica Set Members

The following example ensures the operation has replicated to two members (the primary and one other member):

db.runCommand( { getLastError: 1, w: 2 } )

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.

db.runCommand( { getLastError: 1, w: "majority" } )

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.

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:

db.runCommand( { getLastError: 1, w: 2, wtimeout:5000 } )

When wtimeout is 0, the getLastError operation will never time out.