- Reference >
mongoShell Methods >- Database Methods >
- db.getLastErrorObj()
db.getLastErrorObj()¶
On this page
Definition¶
-
db.getLastErrorObj(w, wtimeout, j)¶ Specifies the level of write concern for confirming the success of previous write operation issued over the same connection and returns the document for that operation.
When using
db.getLastErrorObj(), clients must issue thedb.getLastErrorObj()on the same connection as the write operation they wish to confirm.The
db.getLastErrorObj()is amongoshell wrapper around thegetLastErrorcommand.Changed in version 2.6: A new protocol for write operations integrates write concerns with the write operations, eliminating the need for a separate
db.getLastErrorObj(). Most write methods now return the status of the write operation, including error information. In previous versions, clients typically used thedb.getLastErrorObj()in combination with a write operation to verify that the write succeeded.The
db.getLastErrorObj()can accept the following parameters:Parameter Type Description wint or string Optional. The write concern’s wvalue.wtimeoutint Optional. The time limit in milliseconds. 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 currentcommitIntervalMsbefore writing data to the journal.
Behavior¶
The returned document provides error information on the previous write operation.
If the db.getLastErrorObj() method itself encounters an error, such as an
incorrect write concern value, the db.getLastErrorObj() throws an exception.
For information on the returned document, see getLastError command.
Example¶
The following example issues a db.getLastErrorObj() operation
that verifies that the preceding write operation, issued over the same
connection, has propagated to at least two members of the replica set.
If the last error object is a duplicate key error, the method returns a document similar to the following:
See also