Error Handling in MongoDB DriversΒΆ


This legacy documentation does not necessarily reflect the current practices in MongoDB driver implementation, but may be useful for historical purposes. See the MongoDB Meta Driver for the current documentation of MongoDB driver implementation.

If an error occurs on a query or on a getMore operation sent by a replica set member, MongoDB returns an error object instead of user data.

The error object’s first field has the reserved key $err. For example:

{ $err : "some error message" }

The $err value can be of any type but is usually a string.

Drivers typically check for this return code explicitly and take action rather than return the object to the user. The query results flags include a set bit when $err is returned.

/* db response format

Query or GetMore: // see struct QueryResult
  int resultFlags;
  int64 cursorID;
  int startingFrom;
  int nReturned;
  list of marshalled JSObjects;


struct QueryResult : public MsgData {
  enum {
    ResultFlag_CursorNotFound = 1, /* returned, with zero results, when getMore is called but the cursor id is not valid at the server. */
    ResultFlag_ErrSet = 2          /* { $err : ... } is being returned */

See also

Was this page helpful?

Yes No

Thank you for your feedback!

We're sorry! You can Report a Problem to help us improve this page.