Mongo Wire Protocol(写MongoDB驱动必备)

本文主要介绍了MongoDB的网络协议——Mongo Wire Protocol,包括标准消息头MsgHeader,请求Opcode如OP_QUERY、OP_UPDATE、OP_INSERT、OP_GETMORE和OP_DELETE的结构及其用途。理解该协议是开发MongoDB驱动的关键,例如OP_QUERY用于查询数据库,OP_UPDATE和OP_GET_MORE涉及数据更新和获取,而OP_REPLY是数据库的响应消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目前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 :


 

MsgHeader一个关键的field就是opCode,它决定了Message是干什么用的,比如OP_QUERY肯定是客户端向MongoDB服务器发送的查询请求。

 

  • Request Opcodes

 

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:

 

 

 

MsgHeader是每一个Message必须有的;fullCollectionName就是db + collection name;flags就是一个位描述符,每一位代表的意思如下:

 

 

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:

 

 

 

需要注意的就是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 :

 

 

 

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 :

 

 

 

一般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 :

 

 

 

  • 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:

 

 

 

更多的Opcode请参考:http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值