MongoDB Wire Protocol
- MongoDB Wire Protocol是用于mongo客户端与服务端通信的协议,协议直接基于TCP,
- 建立连接无需应用层的握手
- 所有数据都是小端(Little-Endian)的
标准消息头
所有消息都有相同的消息头
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 }
OpCode有如下:
Name | Value | Comment |
OP_REPLY | 1 | Reply to a client request. responseTo is set. |
OP_MSG | 1000 | Generic msg command followed by a string. |
OP_UPDATE | 2001 | Update document. |
OP_INSERT | 2002 | Insert new document. |
RESERVED | 2003 | Formerly used for OP_GET_BY_OID. |
OP_QUERY | 2004 | Query a collection. |
OP_GET_MORE | 2005 | Get more data from a query. See Cursors. |
OP_DELETE | 2006 | Delete documents. |
OP_KILL_CURSORS | 2007 | Notify database that the client has finished with the cursor. |
OP_COMMAND | 2010 | Cluster internal protocol representing a command request. |
OP_COMMANDREPLY | 2011 | Cluster internal protocol representing a reply to an OP_COMMAND. |
写驱动只用到前9个
客户端请求消息
客户端可以发除了OP_REPLY之外的其它消息给服务端,只有OP_QUERY和OP_GET_MORE有响应消息,其余都没有,可以通过getLastError来判断update、delete之类的命令是否执行成功
OP_UPDATE
update document,格式:
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 }
flags:位向量,以下是每一位的含义:
0:corresponds to Upsert. If set, the database will insert the supplied object into the collection if no matching document is found.
1:corresponds to MultiUpdate.If set, the database will update all matching objects in the collection. Otherwise only updates first matching document.
2-31:reserved,必须为0
OP_INSERT
insert document,格式:
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 }
flags:
0:corresponds to ContinueOnError. If set, the database will not stop processing a bulk insert if one fails (eg due to duplicate IDs). This makes bulk insert behave similarly to a series of single inserts, except lastError will be set if any insert fails, not just the last one. If multiple errors occur, only the most recent will be reported by getLastError.
1-31:reserved,must 0
OP_QUERY
query document,格式:
struct OP_QUERY { MsgHeader header; // standard message header int32 flags; // bit vector of query options. cstring fullCollectionName ; // "dbname.collectionname" int32 numberToSkip; // number of documents to skip,starting from the first document in the resulting dataset int32 numberToReturn; // number of documents to return // in the first OP_REPLY batch document query; // query object. [ document returnFieldsSelector; ] // Optional. Selector indicating the fields // to return. }
flags:
0:reserved,must 0
1:TailableCursor,Tailable means cursor is not closed when the last data is retrieved. Rather, the cursor marks the final object’s position. You can resume using the cursor later, from where it was located, if more data were received. Like any “latent cursor”, the cursor may become invalid at some point (CursorNotFound) – for example if the final object it references were deleted.
2: corresponds to SlaveOk.Allow query of replica slave. Normally these return an error except for namespace “local”.
3:corresponds to OplogReplay. Internal replication use only - driver should not set.
4:corresponds to NoCursorTimeout. The server normally times out idle cursors after an inactivity period (10 minutes) to prevent excess memory use. Set this option to prevent that.
5:corresponds to AwaitData. Use with TailableCursor. If we are at the end of the data, block for a while rather than returning no data. After a timeout period, we do return as normal.
6:corresponds to Exhaust. Stream the data down full blast in multiple “more” packages, on the assumption that the client will fully read all data queried. Faster when you are pulling a lot of data and know you want to pull it all down. Note: the client is not allowed to not read all the data unless it closes the connection.
7:corresponds to Partial. Get partial results from a mongos if some shards are down (instead of throwing an error)
8-31:reserved,must 0
numberToReturn: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 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 use the default return size. If the number is negative, then the database will return that number and close the cursor. No further results for that query can be fetched. If numberToReturn is 1 the server will treat it as -1 (closing the cursor automatically).
OP_QUERY会产生一个OP_REPLY响应
OP_GET_MORE
used to query the database for documents in a collection,格式:
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 }
OP_GET_MORE会产生一个OP_REPLY响应
OP_DELETE
remove document, 格式:
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. }
flags:
0:corresponds to SingleRemove. If set, the database will remove only the first matching document in the collection. Otherwise all matching documents will be removed.
1-31:reserved,must 0
OP_KILL_CURSORS
used to close an active cursor in the database,格式:
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 }
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_MSG
Deprecated 已弃用
OP_COMMAND
这是集群内部消息
以上就是所有的请求消息
服务端响应消息
OP_REPLY
The OP_REPLY message is sent by the database in response to an OP_QUERY or OP_GET_MORE message,格式
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 }
responseFlags:
0:corresponds to CursorNotFound. Is set when getMore is called but the cursor id is not valid at the server. Returned with zero results.
1:corresponds to QueryFailure. Is set when query failed. Results consist of one document containing an “$err” field describing the failure.
2:corresponds to ShardConfigStale. Drivers should ignore this. Only mongos will ever see this set, in which case, it needs to update config from the server.
3:corresponds to AwaitCapable. Is set when the server supports the AwaitData Query option. If it doesn’t, a client should sleep a little between getMore’s of a Tailable cursor. Mongod version 1.6 supports AwaitData and thus always sets AwaitCapable.
4-31:reserved,must 0
OP_COMMANDREPLY
集群内部消息
以上document类型的字段都是BSON类型的,参考http://bsonspec.org/spec.html
相关推荐
- **com.mongodb.protocol**:包含各种协议的实现,如OpQuery(查询操作)、OpInsert(插入操作)等,这些协议是MongoDB Wire Protocol的一部分,用于与MongoDB服务器通信。 3. **util**: 实用工具类的集合,如`...
在实际开发中,驱动程序扮演着至关重要的角色,它们实现了MongoDB Wire Protocol,使得应用能够以高效且兼容的方式与数据库通信。例如,C#驱动提供了`IMongoClient`接口,通过它可以创建数据库实例,执行查询,以及...
1. **MongoDB Wire Protocol**:MongoDB客户端和服务器之间的通信是通过Wire Protocol进行的,它定义了各种操作(如查询、插入、更新、删除等)的数据格式。理解并实现这个协议是构建代理的关键。 2. **Go网络编程*...
它基于Java 6及更高版本,实现了MongoDB的Wire Protocol,允许Java应用与MongoDB进行通信。Java驱动分为几个主要版本,每个版本可能对应不同的功能特性和兼容性。 2. 版本选择: 选择MongoDB Java驱动版本时,应...
这里包含了MongoDB的 wire protocol,它是客户端和服务器之间通信的标准协议。 在安全性方面,MongoDB 3.4引入了SCRAM-SHA-256身份验证协议,增强了密码安全。这部分代码位于`src/mongo/auth`目录下,与认证和授权...
它支持多种网络协议,包括最新的Wire Protocol,以及SSL/TLS加密和安全认证机制,如SCRAM-SHA-1和MONGODB-X509。 3. **libmongocxx**: 这是MongoDB的C++驱动程序,基于libmongoc,但提供了更高级别的C++接口。...
Pymongo是基于MongoDB的Wire Protocol进行封装的,可以跨平台使用,并且支持从版本2.7.1开始的Python 2.x版本,以及Python 3.x版本。 在Linux环境下,比如使用RedHat Enterprise Linux 5-32bit系统,可以通过包管理...
在 "Koko-chat" 中,我们能看到 Meteor 的核心特性得到充分利用,如数据到视图的自动更新(data-on-the-wire)和实时的双向数据绑定。这个项目可能是为教学或实验目的设计的,帮助初学者理解实时聊天应用的基本结构...
Meteor.js的核心理念是"数据即界面"(Data on the Wire),这意味着当数据在服务器上更新时,所有连接到该数据的客户端都会立即自动更新,无需手动刷新页面。这种实时性使得Meteor.js非常适合构建协作工具、社交网络...