- Reference >
- mongo Shell Methods >
- 数据库相关方法 >
- db.getLastError()
db.getLastError()¶
On this page
Definition¶
- db.getLastError(<w>, <wtimeout>)¶
Specifies the level of write concern for confirming the success of previous write operation issued over the same connection and returns the error string for that operation.
When using db.getLastError(), clients must issue the db.getLastError() on the same connection as the write operation they wish to confirm.
在 2.6 版更改: A new protocol for write operations integrates write concerns with the write operations, eliminating the need for a separate db.getLastError(). Most write methods now return the status of the write operation, including error information. In previous versions, clients typically used the db.getLastError() in combination with a write operation to verify that the write succeeded.
The db.getLastError() 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.
Behavior¶
The returned error string provides error information on the previous write operation.
If the db.getLastError() method itself encounters an error, such as an incorrect write concern value, the db.getLastError() throws an exception.
Example¶
The following example issues a db.getLastError() 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.getLastError(2)
参见
getLastError and Write Concern for all options, Write Concern for a conceptual overview, MongoDB CRUD 操作 for information about all write operations in MongoDB.