w3resource

MongoDB: db.getLastError() method

db.getLastError

The db.getLastError() method is used to check and return the status of the last operation.

Syntax:

db.getLastError(>w>, >wtimeout>)

Parameters:

Name Description Required /
Optional
Type
w The write concern’s w value. Optional int or string
wtimeout The time limit in milliseconds. Optional int

Example: MongoDB: db.getLastError() method

The following example will display the error information on the previous write operation.

db.getLastError(2);

Output:

> db.getLastError(2);
2016-01-13T13:09:04.997+0530 getlasterror failed: {
        "connectionId" : 1,
        "n" : 0,
        "badGLE" : {
                "getlasterror" : 1,
                "w" : 2
        },
        "ok" : 0,
        "errmsg" : "cannot use 'w' > 1 when a host is not replicated",
        "code" : 2
} at src/mongo/shell/db.js:533

Retrieve the restaurants data from here

Previous: db.getCollectionNames() method
Next: db.getLastErrorObj() method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mongodb/shell-methods/database/db-getLastError.php