db.getLastErrorObj() in mongoDB

getLastErrorObj(w?, wtimeout?)

This method specifies the level of write concern for confirming the success of a previous write operation issued over the same connection and returns the document for that operation.

Note: When using db.getLastErrorObj(), clients must issue the db.getLastErrorObj() on the same connection as the write operation they wish to confirm.


Syntax:

db.getLastErrorObj(number)


Example 1: When the last error succeed

MongoDB

db.getLastErrorObj(1)

Output:

{
    "connectionId" : 1,
    "n" : 0,
    "syncMillis" : 0,
    "writtenTo" : null,
    "writeConcern" : {
            "w" : 1,
            "wtimeout" : 0
    },
    "err" : null,
    "ok" : 1
}



Example 2: When lasterror failed

MongoDB

db.getLastErrorObj(2)

Output

uncaught exception: Error: 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,
    "codeName" : "BadValue"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getLastErrorObj@src/mongo/shell/db.js:706:15
@(shell):1:1


No comments:

Post a Comment