The Mongo Wire Protocol is a simple socket-based, request-response style protocol. Clients communicate with the database server through a regular TCP/IP socket.
Standard Message Header
In general, each message consists of a standard message header followed by request-specific data. The standard message header is structured as follows :
struct MsgHeader {int32 messageLength; // total message size, including thisint32 requestID; // identifier for this messageint32 responseTo; // requestID from the original request// (used in reponses from db)int32 opCode; // request type - see table below}
The OP_UPDATE message is used to update a document in a collection. The format of a OP_UPDATE message is:
struct OP_UPDATE {MsgHeader header; // standard message headerint32 ZERO; // 0 - reserved for future usecstring fullCollectionName; // "dbname.collectionname"int32 flags; // bit vector. see belowdocument selector; // the query to select the documentdocument update; // specification of the update to perform}
The OP_INSERT message is used to insert one or more documents into a collection. The format of the OP_INSERT message is:
struct {MsgHeader header; // standard message headerint32 flags; // bit vector - see belowcstring fullCollectionName; // "dbname.collectionname"document* documents; // one or more documents to insert into the collection}
The OP_QUERY message is used to query the database for documents in a collection. The format of the OP_QUERY message is :
struct OP_QUERY {MsgHeader header; // standard message headerint32 flags; // bit vector of query options. See below for details.cstring fullCollectionName; // "dbname.collectionname"int32 numberToSkip; // number of documents to skipint32 numberToReturn; // number of documents to return// in the first OP_REPLY batchdocument query; // query object. See below for details.[ document returnFieldSelector; ] // Optional. Selector indicating the fields// to return. See below for details.}
The OP_GETMORE message is used to query the database for documents in a collection. The format of the OP_GETMORE message is :
struct {MsgHeader header; // standard message headerint32 ZERO; // 0 - reserved for future usecstring fullCollectionName; // "dbname.collectionname"int32 numberToReturn; // number of documents to returnint64 cursorID; // cursorID from the OP_REPLY}
The OP_DELETE message is used to remove one or more messages from a collection. The format of the OP_DELETE message is :
struct {MsgHeader header; // standard message headerint32 ZERO; // 0 - reserved for future usecstring fullCollectionName; // "dbname.collectionname"int32 flags; // bit vector - see below for details.document selector; // query object. See below for details.}
Database Response Messages
OP_REPLY
The OP_REPLY message is sent by the database in response to an CONTRIB:OP_QUERY or CONTRIB:OP_GET_MORE
message. The format of an OP_REPLY message is:
struct {MsgHeader header; // standard message headerint32 responseFlags; // bit vector - see details belowint64 cursorID; // cursor id if client needs to do get more'sint32 startingFrom; // where in the cursor this reply is startingint32 numberReturned; // number of documents in the replydocument* documents; // documents}