设为首页 收藏本站
查看: 1180|回复: 0

[经验分享] Mongo Wire Protocol(写MongoDB驱动必备)

[复制链接]

尚未签到

发表于 2015-11-11 07:58:01 | 显示全部楼层 |阅读模式
  目前MongoDB针对各种语言都已经有了相应的驱动,但是写好一个驱动的关键是理解MongoDB提供的协议,下面的内容主要是来自MongoDB的官方的Protocol描述:
  

  • Introduction
  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}
  
  MsgHeader一个关键的field就是opCode,它决定了Message是干什么用的,比如OP_QUERY肯定是客户端向MongoDB服务器发送的查询请求。
  

  • Request Opcodes
DSC0000.gif
  
  Client能够使用除OP_REPLY以外的所有Opcode,而OP_REPLY是留给Mongodb用的;
  Client发送的Opcode中,只有OP_QUERY和OP_GET_MORE会得到MongoDB的response,对于其他的Opcode一概不给予答复(如果你真的需要看你发送的opcode是否执行成功,可以调用getLastError查看);

  • Client Request Messages
  OP_UPDATE
  
  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}
  
  MsgHeader是每一个Message必须有的;fullCollectionName就是db + collection name;flags就是一个位描述符,每一位代表的意思如下:
  
DSC0001.gif
  
  Upsert就是当要更新的document不存在的时候,是否将其插入到collection中去,MultiUpdate则表示当selector找到了多个符合更新条件的document时,是否将它们全部更新; selector决定了选择哪些document去做更新操作,它也是经常用在find中的;update则表示要将找到的document更新成什么模样,也能是一部分field,或者全部field。
  
  OP_INSERT
  
  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}
  
  需要注意的就是flags每位代表的意思改变了,具体查看:http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol
  
  OP_QUERY
  
  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.}
  
  numberToSkip有skip参数决定了,表示要对查找到的document跳过多少个;numberToReturn则由limit参数决定了,表示要返回多少个document给client;query就是个查询条件;returnFieldSelector则表示返回哪些docuemnt的fields回来,因为我们也有是需要document的部分field的情况,而不是所有的fields。
  
  OP_GETMORE
  
  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}
  
  一般mongoDB做查询的时候返回的是Cursor对象,它并不包含实际的数据,而只是数据的一些标识,当我们需要具体的document的时候可以调用Cursor的方法来向mongoDB请求数据,这个时候发送的Opcode就是OP_GETMORE了,所以cursorID就是告诉MongoDB返回从哪个位置开始的数据罢了。
  
  OP_DELETE
  
  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}
  
  更多的Opcode请参考:http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol
             版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-137674-1-1.html 上篇帖子: Mongo DB的分析和应用场景 下篇帖子: mongo db 安装及常用命令
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表