What is the MongoDB wire protocol?

Answer

The MongoDB Wire Protocol is the binary protocol used for communication between MongoDB clients (drivers) and the MongoDB server over TCP. It defines the message format for all database operations. Key aspects: (1) TCP-based: runs over standard TCP (default port 27017); (2) OP_MSG: the current primary message format (replaced older OP_QUERY, OP_INSERT, etc. in MongoDB 3.6). Uses BSON documents for both request and response; (3) Message structure: header (length, requestId, responseTo, opCode) + body (BSON document with operation details); (4) Compression: wire protocol supports zlib, Snappy, and zstd compression to reduce network bandwidth; (5) Authentication: SCRAM-SHA-1, SCRAM-SHA-256, MONGODB-X509, GSSAPI (Kerberos), PLAIN (LDAP) authentication mechanisms; (6) TLS/SSL: optional encryption of the wire protocol for security. MongoDB drivers: handle the wire protocol for you — you never interact with it directly. Drivers exist for all major languages: Node.js, Python (PyMongo), Java (MongoDB Java Driver), Go, C#, PHP, Ruby, Rust, etc. Connection pooling: drivers maintain connection pools to MongoDB — multiple socket connections to the server, reused across requests. Pool size is configurable (default: 100 per MongoClient). Create ONE MongoClient per application and reuse it — don't create/destroy per request.