IntroductionThe Mongo Wire Protocol is a simple socket-based, request-response style protocol. Clients communicate with the database server through a regular TCP/IP socket.
Clients should connect to the database with a regular TCP/IP socket. Currently, there is no connection handshake.
Messages Types and FormatsThere are two types of messages, client requests and database responses, each having a slightly different structure. Standard Message HeaderIn general, each message consists of a standard message header followed by request-specific data. The standard message header is structured as follows : struct {
int32 messageLength; // total size of the message, including the 4 bytes of length
int32 requestID; // client or database-generated identifier for this message
int32 responseTo; // requestID from the original request (used in reponses from db)
int32 opCode; // request type - see table below
}
messageLength : This is the total size of the message in bytes. This total includes the 4 bytes that holds the message length. requestID : This is a client or database-generated identifier that uniquely identifies this message. For the case of client-generated messages (e.g. CONTRIB:OP_QUERY and CONTRIB:OP_GET_MORE), it will be returned in the responseTo : In the case of a message from the database, this will be the requestID taken from the CONTRIB:OP_QUERY or CONTRIB:OP_GET_MORE messages from the client. Along with the requestID field in queries, clients can use this to associate query responses with the originating query. opCode : Type of message. See the table below in the next section. Request OpcodesThe following are the currently supported opcodes :
Client Request MessagesClients can send all messages except for CONTRIB:OP_REPLY. This is reserved for use by the database. Note that only the CONTRIB:OP_QUERY and CONTRIB:OP_GET_MORE messages result in a response from the database. There will be no response sent for any other message. You can determine if a message was successful with a $$$ TODO get last error command. OP_UPDATEThe OP_UPDATE message is used to update a document in a collection. The format of a OP_UPDATE message is struct {
MsgHeader header; // standard message header
int32 ZERO; // 0 - reserved for future use
cstring fullCollectionName; // "dbname.collectionname"
int32 flags; // bit vector. see below
BSON selector; // the query to select the document
BSON document; // the document data to update with or insert
}
fullCollectionName : The full collection name. The full collection name is the concatenation of the database name with the collection name, using a "." for the concatenation. For example, for the database "foo" and the collection "bar", the full collection name is "foo.bar". flags :
selector : BSON document that specifies the query for selection of the document to update. document : BSON document that specifies the fields to change in the selected document, or in the case of an upsert, the document to insert into the collection. There is no response to an OP_UPDATE message. OP_INSERTThe 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 header
int32 ZERO; // 0 - reserved for future use
cstring fullCollectionName; // "dbname.collectionname"
BSON[] documents; // one or more documents to insert into the collection
}
fullCollectionName : The full collection name. The full collection name is the concatenation of the database name with the collection name, using a "." for the concatenation. For example, for the database "foo" and the collection "bar", the full collection name is "foo.bar". documents : One or more documents to insert into the collection. If there are more than one, they are written to the socket in sequence, one after another. There is no response to an OP_UPDATE message. OP_QUERYThe OP_QUERY message is used to query the database for documents in a collection. The format of the OP_QUERY message is : struct {
MsgHeader header; // standard message header
int32 opts; // query options. See below for details.
cstring fullCollectionName; // "dbname.collectionname"
int32 numberToSkip; // number of documents to skip when returning results
int32 numberToReturn; // number of documents to return in the first OP_REPLY
BSON query ; // query object. See below for details.
[ BSON returnFieldSelector; ] // OPTIONAL : selector indicating the fields to return. See below for details.
}
opts : query options
fullCollectionName : The full collection name. The full collection name is the concatenation of the database name with the collection name, using a "." for the concatenation. For example, for the database "foo" and the collection "bar", the full collection name is "foo.bar". numberToSkip : Sets the number of documents to omit - starting from the first document in the resulting dataset - when returning the result of the query. numberToReturn : Limits the number of documents in the first CONTRIB:OP_REPLY message to the query. However, the database will still establish a cursor and return the cursorID to the client if there are more results than numberToReturn. If the client driver offers 'limit' functionality (like the SQL LIMIT keyword), then it is up to the client driver to ensure that no more than the specified number of document are returned to the calling application. If numberToReturn is 0, the db will used the default return size. If the number is negative, then the database will return that number and close the cursor. No futher results for that query can be fetched. query : BSON document that represents the query. The query will contain one or more elements, all of which must match for a document to be included in the result set. Please see $$$ TODO QUERY for more information. returnFieldsSelector : OPTIONAL BSON document that limits the fields in the returned documents. The returnFieldsSelector contains one or more elements, each of which is the name of a field that should be returned, and and the integer value 1. In JSON notation, a returnFieldsSelector to limit to the fields "a", "b" and "c" would be : { a : 1, b : 1, c : 1}
The database will respond to an OP_QUERY message with an CONTRIB:OP_REPLY message. OP_GETMOREThe 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 header
int32 ZERO; // 0 - reserved for future use
cstring fullCollectionName; // "dbname.collectionname"
int32 numberToReturn; // number of documents to return
int64 cursorID; // cursorID from the OP_REPLY
}
fullCollectionName : The full collection name. The full collection name is the concatenation of the database name with the collection name, using a "." for the concatenation. For example, for the database "foo" and the collection "bar", the full collection name is "foo.bar". numberToReturn : Limits the number of documents in the first CONTRIB:OP_REPLY message to the query. However, the database will still establish a cursor and return the cursorID to the client if there are more results than numberToReturn. If the client driver offers 'limit' functionality (like the SQL LIMIT keyword), then it is up to the client driver to ensure that no more than the specified number of document are returned to the calling application. If numberToReturn is 0, the db will used the default return size. cursorID : Cursor identifier that came in the CONTRIB:OP_REPLY. This must be the value that came from the database. The database will respond to an OP_GETMORE message with an CONTRIB:OP_REPLY message. OP_DELETEThe 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 header
int32 ZERO; // 0 - reserved for future use
cstring fullCollectionName; // "dbname.collectionname"
int32 ZERO; // 0 - reserved for future use
BSON selector; // query object. See below for details.
}
fullCollectionName : The full collection name. The full collection name is the concatenation of the database name with the collection name, using a "." for the concatenation. For example, for the database "foo" and the collection "bar", the full collection name is "foo.bar". selector : BSON document that represent the query used to select the documents to be removed. The selector will contain one or more elements, all of which must match for a document to be removed from the collection. Please see $$$ TODO QUERY for more information. There is no reponse to an OP_DELETE message. OP_KILL_CURSORSThe OP_KILL_CURSORS message is used to close an active cursor in the database. This is necessary to ensure that database resources are reclaimed at the end of the query. The format of the OP_KILL_CURSORS message is : struct {
MsgHeader header; // standard message header
int32 ZERO; // 0 - reserved for future use
int32 numberOfCursorIDs; // number of cursorIDs in message
int64[] cursorIDs; // array of cursorIDs to close
}
numberOfCursorIDs : The number of cursors that are in the message. cursorIDs : "array" of cursor IDs to be closed. If there are more than one, they are written to the socket in sequence, one after another. Note that if a cursor is read until exhausted (read until OP_QUERY or OP_GETMORE returns zero for the cursor id), there is no need to kill the cursor. OP_MSGDeprecated. OP_MSG sends a diagnostic message to the database. The database sends back a fixed resonse. The format is struct {
MsgHeader header; // standard message header
cstring message; // message for the database
}
Drivers do not need to implement OP_MSG. Database Response MessagesOP_REPLYThe 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 header
int32 responseFlag; // normally zero, non-zero on query failure
int64 cursorID; // id of the cursor created for this query response
int32 startingFrom; // indicates where in the cursor this reply is starting
int32 numberReturned; // number of documents in the reply
BSON[] documents; // documents
}
responseFlag : Flag that indicates status of the query. Normally 0, a non-zero value indicates that the query failed for some reason. In the event of a non-zero responseFlag only one document will be returned, a database-generated document that contains error information. $$$ TODO DOCUMENT THAT. cursorID : The cursorID that this OP_REPLY is a part of. In the event that the result set of the query fits into one OP_REPLY message, cursorID will be 0. This cursorID must be used in any CONTRIB:OP_GET_MORE messages used to get more data, and also must be closed by the client when no longer needed via a CONTRIB:OP_KILL_CURSORS message. TODO : 1) describe object id |

IF YOU HAVE A QUESTION, POST IT TO THE USER GROUP.
These pages are fine for comments, but for questions, your best bet will always be the MongoDB User Group. blog comments powered by Disqus