MongoDB Wire Protocol
On this page
Note
This MongoDB Wire Protocol Specification is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License. You may not use or adapt this material for any commercial purpose, such as to create a commercial database or database-as-a-service offering.
Introduction
The MongoDB Wire Protocol is a simple socket-based, request-response style protocol. Clients communicate with the database server through a regular TCP/IP socket.
TCP/IP Socket
Clients should connect to the database with a regular TCP/IP socket.
Port
The default port number for mongod
and mongos
instances is 27017. The port number for mongod
and
mongos
is configurable and may vary.
Byte Ordering
All integers in the MongoDB wire protocol use little-endian byte order: that is, least-significant byte first.
Messages Types and Formats
MongoDB uses the OP_MSG opcode for both client
requests and database replies. There are several message formats used in
older versions of MongoDB which have been deprecated in favor of
OP_MSG
.
Note
This page uses a C-like
struct
to describe the message structure.The types used in this document, such as
int32
andcstring
, are the same as those defined in the BSON specification.To denote repetition, the document uses the asterisk notation from the BSON specification. For example,
int64*
indicates that one or more of the specified type can be written to the socket, one after another.The standard message header is typed as
MsgHeader
. Integer constants are in capitals (e.g.ZERO
for the integer value of 0).
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 this int32 requestID; // identifier for this message int32 responseTo; // requestID from the original request // (used in responses from db) int32 opCode; // request type - see table below for details }
Field | Description |
---|---|
| The total size of the message in bytes. This total includes the 4 bytes that holds the message length. |
| A client or database-generated identifier that uniquely
identifies this message. For the case of client-generated
messages (e.g. OP_QUERY and
OP_GET_MORE), it will be returned in
the |
| In the case of a message from the database, this will be the
|
| Type of message. See Request Opcodes for details. |
Request Opcodes
Note
Starting with MongoDB 2.6 and
maxWireVersion
3
, MongoDB drivers use the database commandsinsert
,update
, anddelete
instead ofOP_INSERT
,OP_UPDATE
, andOP_DELETE
for acknowledged writes. Most drivers continue to use opcodes for unacknowledged writes.In version 4.2, MongoDB removes the deprecated internal
OP_COMMAND
andOP_COMMANDREPLY
protocol.In version 5.0, MongoDB deprecates the following opcodes:
OP_REPLY
OP_UPDATE
OP_INSERT
OP_QUERY
[1]OP_GET_MORE
OP_DELETE
OP_KILL_CURSORS
Instead of these opcodes, use OP_MSG.
[1] MongoDB 5.0 deprecates both OP_QUERY
find operations andOP_QUERY
commands. As an exception,OP_QUERY
is still supported for running thehello
andisMaster
commands as part of the connection handshake.
MongoDB supports these opCode
values:
Opcode Name | Value | Comment |
---|---|---|
| 2013 | Send a message using the format introduced in MongoDB 3.6. |
OP_REPLY Deprecated in MongoDB 5.0. | 1 | Reply to a client request. |
OP_UPDATE Deprecated in MongoDB 5.0. | 2001 | Update document. |
OP_INSERT Deprecated in MongoDB 5.0. | 2002 | Insert new document. |
| 2003 | Formerly used for OP_GET_BY_OID. |
OP_QUERY Deprecated in MongoDB 5.0. | 2004 | Query a collection. |
OP_GET_MORE Deprecated in MongoDB 5.0. | 2005 | Get more data from a query. See Cursors. |
OP_DELETE Deprecated in MongoDB 5.0. | 2006 | Delete documents. |
OP_KILL_CURSORS Deprecated in MongoDB 5.0. | 2007 | Notify database that the client has finished with the cursor. |
| 2012 | Wraps other opcodes using compression |
Client Request Messages
OP_MSG
OP_MSG
is an extensible message format designed to subsume the
functionality of other opcodes. This opcode has the following format:
OP_MSG { MsgHeader header; // standard message header uint32 flagBits; // message flags Sections[] sections; // data sections optional<uint32> checksum; // optional CRC-32C checksum }
Field | Description |
---|---|
| Standard message header, as described in Standard Message Header. |
| An integer bitmask containing message flags, as described in Flag Bits. |
| Message body sections, as described in Sections. |
| An optional CRC-32C checksum, as described in Checksum. |
Flag Bits
The flagBits
integer is a bitmask encoding flags that modify the
format and behavior of OP_MSG
.
The first 16 bits (0-15) are required and parsers MUST error if an unknown bit is set.
The last 16 bits (16-31) are optional, and parsers MUST ignore any unknown set bits. Proxies and other message forwarders MUST clear any unknown optional bits before forwarding messages.
Bit | Name | Request | Response | Description |
---|---|---|---|---|
0 |
| ✓ | ✓ | |
1 |
| ✓ | ✓ | Another message will follow this one without further action from
the receiver. The receiver MUST NOT send another message until
receiving one with |
16 |
| ✓ | The client is prepared for multiple replies to this request using
the This ensures that multiple replies are only sent when the network layer of the requester is prepared for them. ImportantMongoDB 3.6 ignores this flag, and will respond with a single message. |
Sections
An OP_MSG
message contains one or more sections. Each section starts
with a kind
byte indicating its type. Everything after the kind
byte constitutes the section's payload.
The available kinds of sections follow.
Kind 0: Body
A body section is encoded as a single BSON object. The size in the BSON object also serves as the size of the section. This section kind is the standard command request and reply body.
All top-level fields MUST have a unique name.
Kind 1: Document Sequence
Type | Description |
---|---|
int32 | Size of the section in bytes. |
C String | Document sequence identifier. In all current commands this field is the (possibly nested) field that it is replacing from the body section. This field MUST NOT also exist in the body section. |
Zero or more BSON objects |
|
Checksum
Each message MAY end with a CRC-32C [2] checksum that covers all bytes in the message except for the checksum itself.
Starting in MongoDB 4.2:
mongod
instances,mongos
instances, andmongo
shell instances will exchange messages with checksums if not using TLS/SSL connection.mongod
instances,mongos
instances, andmongo
shell instances will skip the checksum if using TLS/SSL connection.
Drivers and older binaries will ignore the checksum if presented with messages with checksum.
The presence of a checksum is indicated by the checksumPresent
flag
bit.
OP_UPDATE
Deprecated in MongoDB 5.0.
The OP_UPDATE message is used to update a document in a collection. The format of a OP_UPDATE message is the following:
struct OP_UPDATE { MsgHeader header; // standard message header int32 ZERO; // 0 - reserved for future use cstring fullCollectionName; // "dbname.collectionname" int32 flags; // bit vector. see below document selector; // the query to select the document document update; // specification of the update to perform }
Field | Description |
---|---|
| Message header, as described in Standard Message Header. |
| Integer value of 0. Reserved for future use. |
| The full collection name; i.e. namespace. The full collection
name is the concatenation of the database name with the
collection name, using a |
| Bit vector to specify flags for the operation. The bit values correspond to the following:
|
| BSON document that specifies the query for selection of the document to update. |
| BSON document that specifies the update to be performed. For information on specifying updates see the Update Operations documentation. |
There is no response to an OP_UPDATE message.
OP_INSERT
Deprecated in MongoDB 5.0.
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 header int32 flags; // bit vector - see below cstring fullCollectionName; // "dbname.collectionname" document* documents; // one or more documents to insert into the collection }
Field | Description |
---|---|
| Message header, as described in Standard Message Header. |
| Bit vector to specify flags for the operation. The bit values correspond to the following:
|
| The full collection name; i.e. namespace. The full collection
name is the concatenation of the database name with the
collection name, using a |
| 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_INSERT message.
OP_QUERY
Deprecated in MongoDB 5.0.
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 header int32 flags; // bit vector of query options. See below for details. cstring fullCollectionName ; // "dbname.collectionname" int32 numberToSkip; // number of documents to skip int32 numberToReturn; // number of documents to return // in the first OP_REPLY batch document query; // query object. See below for details. [ document returnFieldsSelector; ] // Optional. Selector indicating the fields // to return. See below for details. }
Field | Description | |
---|---|---|
| Message header, as described in Standard Message Header. | |
| Bit vector to specify flags for the operation. The bit values correspond to the following:
| |
| The full collection name; i.e. namespace. The full collection
name is the concatenation of the database name with the
collection name, using a | |
| Sets the number of documents to omit - starting from the first document in the resulting dataset - when returning the result of the query. | |
| Limits the number of documents in the first OP_REPLY message to the query. However, the database
will still establish a cursor and return the | |
| 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. Possible elements include
| |
| Optional. BSON document that limits the fields in the returned
documents. The
|
The database will respond to an OP_QUERY message with an OP_REPLY message.
OP_GET_MORE
Deprecated in MongoDB 5.0.
The OP_GET_MORE message is used to query the database for documents in a collection. The format of the OP_GET_MORE 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 }
Field | Description |
---|---|
| Message header, as described in Standard Message Header. |
| Integer value of 0. Reserved for future use. |
| The full collection name; i.e. namespace. The full collection
name is the concatenation of the database name with the
collection name, using a |
| Limits the number of documents in the first OP_REPLY message to the query. However, the database
will still establish a cursor and return the |
| Cursor identifier that came in the OP_REPLY. This must be the value that came from the database. |
The database will respond to an OP_GET_MORE message with an OP_REPLY message.
OP_DELETE
Deprecated in MongoDB 5.0.
The OP_DELETE message is used to remove one or more documents 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 flags; // bit vector - see below for details. document selector; // query object. See below for details. }
Field | Description |
---|---|
| Message header, as described in Standard Message Header. |
| Integer value of 0. Reserved for future use. |
| The full collection name; i.e. namespace. The full collection
name is the concatenation of the database name with the
collection name, using a |
| Bit vector to specify flags for the operation. The bit values correspond to the following:
|
| 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. |
There is no response to an OP_DELETE message.
OP_KILL_CURSORS
Deprecated in MongoDB 5.0.
The 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; // sequence of cursorIDs to close }
Field | Description |
---|---|
| Message header, as described in Standard Message Header. |
| Integer value of 0. Reserved for future use. |
| The number of cursor IDs that are in the message. |
| "Array" of cursor IDs to be closed. If there are more than one, they are written to the socket in sequence, one after another. |
If a cursor is read until exhausted (read until OP_QUERY or OP_GET_MORE returns zero for the cursor id), there is no need to kill the cursor.
OP_COMPRESSED
Any opcode can be compressed and wrapped in an OP_COMPRESSED header. The OP_COMPRESSED message contains the original compressed opcode message alongside the metadata necessary to process and decompress it.
The format of the OP_COMPRESSED message is:
struct { MsgHeader header; // standard message header int32 originalOpcode; // value of wrapped opcode int32 uncompressedSize; // size of deflated compressedMessage, excluding MsgHeader uint8 compressorId; // ID of compressor that compressed message char *compressedMessage; // opcode itself, excluding MsgHeader }
Field | Description |
---|---|
| Message header, as described in Standard Message Header. |
| Contains the value of the wrapped opcode. |
| The size of the deflated |
| The ID of the compressor that compressed the message. A list of
|
| The opcode itself, excluding the |
Each compressor is assigned a predefined compressor ID as follows:
compressorId | Handshake Value | Description |
---|---|---|
| noop | The content of the message is uncompressed. This is used for testing. |
| snappy | The content of the message is compressed using snappy. |
| zlib | The content of the message is compressed using zlib. |
| zstd | The content of the message is compressed using zstd. |
| reserved | Reserved for future use. |
Database Response Messages
OP_REPLY
Deprecated in MongoDB 5.0.
The OP_REPLY
message is sent by the database in response to an
OP_QUERY or OP_GET_MORE message. The format of an OP_REPLY message is:
struct { MsgHeader header; // standard message header int32 responseFlags; // bit vector - see details below int64 cursorID; // cursor id if client needs to do get more's int32 startingFrom; // where in the cursor this reply is starting int32 numberReturned; // number of documents in the reply document* documents; // documents }
Field | Description |
---|---|
| Message header, as described in Standard Message Header. |
| Bit vector to specify flags. The bit values correspond to the following:
|
| The |
| Starting position in the cursor. |
| Number of documents in the reply. |
| Returned documents. |
Footnotes
[2] | (1, 2) 32-bit CRC computed with the Castagnoli polynomial as described by https://tools.ietf.org/html/rfc4960#page-140. |