Docs Menu

Docs HomeMongoDB Manual

db.getLastErrorObj()

On this page

  • Definition
  • Behavior
  • Example
db.getLastErrorObj(w, wtimeout, j)

Starting in MongoDB 5.0, db.getLastErrorObj() is deprecated. For more information and alternatives, see Deprecated Wire Protocol Opcodes.

The db.getLastErrorObj() can accept the following parameters:

Parameter
Type
Description
w
int or string
Optional. The write concern's w value.
wtimeout
int
Optional. The time limit in milliseconds.
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.

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.

The db.getLastErrorObj() is a shell wrapper around the getLastError command.

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.

db.getLastErrorObj(2)

If the last error object is a duplicate key error, the method returns a document similar to the following:

{
"err" : "E11000 duplicate key error collection: test.foobar index: x_1 dup key: { x: 1.0 }",
"ns" : "test.foobar", // New in 4.4
"index" : "x_1", // New in 4.4
"code" : 11000,
"codeName" : "DuplicateKey",
"n" : 0,
"singleShard" : "m1.example.net:27017",
"ok" : 1,
"operationTime" : Timestamp(1584654273, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1584654274, 1),
"signature" : {
"hash" : BinData(0,"Z3L9FdEIYQ2MwLg6X/4sDWacqzM="),
"keyId" : NumberLong("6806025526243754005")
}
}
}

Tip

See also:

←  db.getLastError()db.getLogComponents() →
Share Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.