对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

传统操作码

本页介绍 MongoDB 不再支持的传统操作码。这些传统操作码:

  • 从 MongoDB 5.0 起已弃用。

  • 从 MongoDB 5.1 开始不支持。

从 MongoDB 5.1 开始,OP_MSGOP_COMPRESSED 是向 MongoDB Server 发送请求时唯一支持的操作码。

OP_DELETE 消息用于从集合中删除一个或多个文档。OP_DELETE 消息的格式为:

struct {
MsgHeader header; // standard message header
int32 ZERO; // 0 - reserved for future use
cstring fullCollectionName; // "dbname.collectionname"
int32 flags; // bit values - see below for details.
document selector; // query object. See below for details.
}
字段
说明

header

消息头。请参阅标准消息头

ZERO

整数值 0。保留供将来使用。

fullCollectionName

完整的集合名称,特别是其命名空间。命名空间是数据库名称与集合名称的串联,使用 . 表示串联。例如,对于数据库 test 和集合 contacts,完整的集合名称为 test.contacts

flags

操作的位值:位值对应于以下内容:

  • 0 对应于 SingleRemove。如果设置此选项,数据库将仅删除集合中第一个匹配的文档。否则,数据库将删除所有匹配的文档。

  • 1-31 保留。必须设置为 0。

selector

BSON 文档,代表选择要删除的文档的查询。selector 包含一个或多个元素,所有这些元素必须与要从集合中删除的文档匹配。

对 OP_DELETE 消息没有响应。

OP_GET_MORE 消息用于在数据库中查询集合的文档。OP_GET_MORE 消息的格式为:

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
}
字段
说明

header

消息头。请参阅标准消息头

ZERO

整数值 0。保留供将来使用。

fullCollectionName

完整的集合名称,特别是其命名空间。命名空间是数据库名称与集合名称的串联,使用 . 表示串联。例如,对于数据库 test 和集合 contacts,完整的集合名称为 test.contacts

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.

如果 numberToReturn 是:

  • 0,则数据库使用默认的返回大小。

  • 为负数时,数据库会返回该数字并关闭游标。无法获取该查询的其他结果。

  • 1,服务器会将该值视为 -1(自动关闭游标)。

cursorID

OP_REPLY 中的游标标识符。它必须是来自数据库的值。

数据库将使用 OP_REPLY 消息来响应 OP_GET_MORE 消息。

OP_DELETE 消息用于向集合中插入一个或多个文档。OP_INSERT 消息的格式为:

struct {
MsgHeader header; // standard message header
int32 flags; // bit values - see below
cstring fullCollectionName; // "dbname.collectionname"
document* documents; // one or more documents to insert into the collection
}
字段
说明

header

消息头。请参阅标准消息头

flags

操作的位值:位值对应于以下内容:

  • 0 对应于 ContinueOnError。如果设置此选项,则即使批量插入失败(如由于 ID 重复),数据库也不会停止处理批量插入。这使得批量插入的行为类似于一系列单次插入,只是任何插入失败而不仅仅是最后插入失败都会设置 lastError。如果出现多个错误,getLastError 只报告最新的错误。

  • 1-31 保留。必须设置为 0。

fullCollectionName

完整的集合名称,特别是其命名空间。命名空间是数据库名称与集合名称的串联,使用 . 表示串联。例如,对于数据库 test 和集合 contacts,完整的集合名称为 test.contacts

documents

要插入集合的一个或多个文档。如果有多个文档,则按顺序依次写入套接字。

对 OP_INSERT 消息没有响应。

OP_KILL_CURSORS 消息用于关闭数据库中的活动游标。这是必要的操作,用于确保在查询结束时回收数据库资源。OP_KILL_CURSORS 消息的格式为:

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
}
字段
说明

header

消息头。请参阅标准消息头

ZERO

整数值 0。保留供将来使用。

numberOfCursorIDs

消息中的游标 ID 的数量。

cursorIDs

要关闭的游标 ID 的“数组”。如果有多个操作码,则按顺序依次写入套接字。

如果读取游标直至耗尽(读取直至 OP_QUERYOP_GET_MORE 返回游标 ID 为零),则无需终止游标。

OP_QUERY 消息用于在数据库中查询集合中的文档。OP_QUERY 消息的格式为:

struct OP_QUERY {
MsgHeader header; // standard message header
int32 flags; // bit values 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.
}
字段
说明

header

消息头。请参阅标准消息头

flags

操作的位值:位值对应于以下内容:

  • 0 已保留。必须设置为 0。

  • 1 对应于 TailableCursor。Tailable 表示检索最后一个数据时游标未关闭。相反,游标标记的是最终对象的位置。如果接收到更多数据,您可以稍后从游标所在的位置继续使用游标。与任何潜在游标一样,游标可能在某个时刻变得无效 (CursorNotFound) – 例如,如果它最终引用的对象被删除。

  • 2 对应于 SlaveOk。允许查询从属副本。通常,除了命名空间“local”之外,它们都会返回错误。

  • 3 对应于 OplogReplay。 您无需指定此标志,因为 oplog 上符合条件的查询会自动进行优化。 有关更多信息,请参阅oplogReplay

  • 4 对应于 NoCursorTimeout。服务器通常会在不活动期(10 分钟)后使空闲游标超时,防止过度使用内存。设置此选项,可防止出现这种情况。

  • 5 对应于 AwaitData。与 TailableCursor 一起使用。如果游标位于数据末尾,则会阻塞一段时间,而不是不返回数据。超时一段时间后,服务器正常返回。

  • 6 对应于 Exhaust。假设客户端将完全读取所有查询的数据,则将以多个“更多”包的形式全速下传数据。当您拉取大量数据并知道要将其全部拉取时,速度会更快。注意:除非客户端关闭连接,否则不允许不读取所有数据。

  • 7 对应于部分。如果某些分片不可用,则从 mongos 获取部分结果(而不是引发错误)

  • 8-31 保留。必须设置为 0。

fullCollectionName

完整的集合名称,特别是其命名空间。命名空间是数据库名称与集合名称的串联,使用 . 表示串联。例如,对于数据库 test 和集合 contacts,完整的集合名称为 test.contacts

numberToSkip

设置返回查询结果时要忽略的文档数量(从结果数据集的第一个文档开始)。

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.

如果 numberToReturn 是:

  • 0,则数据库使用默认的返回大小。

  • 为负数时,数据库会返回该数字并关闭游标。无法获取该查询的其他结果。

  • 1,服务器会将该值视为 -1(自动关闭游标)。

query

表示查询的 BSON 文档。查询包含一个或多个元素,所有这些元素都必须与要包含在结果集中的文档相匹配。可能的元素包括 $query$orderby$hint$explain

returnFieldsSelector

可选。用于限制已返回文档中的字段的 BSON 文档。returnFieldsSelector 包含一个或多个元素,每个元素都是应返回的字段的名称以及整数值 1。在 JSON 表示法中,限制为字段 abcreturnFieldsSelector 为:

{ a : 1, b : 1, c : 1}

数据库将使用 OP_QUERY 消息来响应 OP_REPLY 消息。

注意

MongoDB 5.1 已删除对 OP_QUERY 查找操作和 OP_QUERY 命令的支持。但有一例外:运行 helloisMaster 命令以作为连接握手的一部分仍然支持 OP_QUERY

OP_REPLY消息由数据库发送,以响应 OP_QUERYOP_GET_MORE 消息。OP_REPLY 消息的格式为:

struct {
MsgHeader header; // standard message header
int32 responseFlags; // bit values - 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
}
字段
说明

header

消息头。请参阅标准消息头

responseFlags

操作的位值:位值对应于以下内容:

  • 0 对应于 cursorNotFound。当调用了 getMore 但游标 ID 在服务器上无效时设置。返回结果为零。

  • 1 对应于 QueryFailure。在查询失败时设置。结果由一个包含描述失败的“$err”字段的文档组成。

  • 2 对应于 ShardConfigStale。驱动程序应忽略这一点。只有 mongos 会看到此集合,在这种情况下,它需要从服务器更新配置。

  • 3 对应于 AwaitCapable。当服务器支持 AwaitData 查询选项时设置。如果没有,则客户端应在可追加游标的 getMore 之间休息一下。

  • 4-31 保留。忽略。

cursorID

cursorID此 OP_REPLY 是 的一部分。事件查询的结果设立适合一条 OP_REPLY 消息,cursorID 将为0 。此cursorID 必须在用于获取更多数据的任何 OP_GET_MORE 消息中使用,并且还必须由客户端在不再需要时通过 OP_KIL_CURSORS 消息关闭。

startingFrom

游标的起始位置。

numberReturned

回复中的文档数量。

documents

已返回文档。

OP_UPDATE 消息用于更新集合中的文档。OP_UPDATE 消息的格式如下:

struct OP_UPDATE {
MsgHeader header; // standard message header
int32 ZERO; // 0 - reserved for future use
cstring fullCollectionName; // "dbname.collectionname"
int32 flags; // bit values. see below
document selector; // the query to select the document
document update; // specification of the update to perform
}
字段
说明

header

消息头。请参阅标准消息头

ZERO

整数值 0。保留供将来使用。

fullCollectionName

完整的集合名称,特别是其命名空间。命名空间是数据库名称与集合名称的串联,使用 . 表示串联。例如,对于数据库 test 和集合 contacts,完整的集合名称为 test.contacts

flags

操作的位值:位值对应于以下内容:

  • 0 对应于 Upsert。设置此选项,则在未找到匹配的文档时,数据库会将提供的对象插入集合。

  • 1 对应于 MultiUpdate。如果设置此选项,数据库将更新集合中的所有匹配对象。否则,仅更新第一个匹配的文档。

  • 2-31 保留。必须设置为 0。

selector

BSON 文档,指定选择要更新的文档的查询。

update

BSON 文档,指定要执行的更新。有关指定更新的信息,请参阅更新操作文档。

对 OP_UPDATE 消息没有响应。