配置副本集上的操作
概述
在本指南中,您可以了解如何使用 写关注、读关注 和 读偏好 配置来修改 MongoDB 在副本集上运行创建、读取、更新和删除 (CRUD) 操作的方式。
您可以在以下级别设置这些配置
客户端,它为所有操作执行设置默认值,除非被覆盖
事务
数据库
集合
上述列表按优先级顺序排列。例如,如果您在客户端和数据库级别都设置了读关注,则数据库级别的读关注将覆盖客户端级别的读关注。
写关注
写关注指定在操作成功返回之前从 MongoDB 请求的确认级别。没有指定显式写关注的操作继承全局默认写关注设置。
您可以通过调用以下函数来设置写关注
mongoc_client_set_write_concern()
在客户端上设置写关注。mongoc_transaction_opts_set_write_concern()
在事务上设置写关注。mongoc_database_set_write_concern()
在数据库上设置写关注。mongoc_collection_set_write_concern()
在集合上设置写关注。
要指定写关注的级别,请调用 mongoc_write_concern_set_w()
函数,并传递您的写关注和以下值之一
MONGOC_WRITE_CONCERN_W_DEFAULT
:写操作在操作写入内存后返回。0:写操作在主节点处理写操作后返回。
1:写操作仅在主节点确认写操作后返回,而不等待从节点确认。
MONGOC_WRITE_CONCERN_W_MAJORITY
:写操作在多数副本集成员确认写操作后返回。MONGOC_WRITE_CONCERN_W_TAG
:写操作在具有指定标签的副本集成员确认写操作后返回。
以下示例将写入关注设置为 MONGOC_WRITE_CONCERN_W_MAJORITY
,用于 mongoc_client_t
实例。
// Create a new client instance mongoc_client_t *client = mongoc_client_new ("<connection string>"); // Create a new write concern mongoc_write_concern_t *write_concern = mongoc_write_concern_new (); mongoc_write_concern_set_w (write_concern, MONGOC_WRITE_CONCERN_W_MAJORITY); // Set the write concern on the client mongoc_client_set_write_concern (client, write_concern);
以下示例将写入关注设置为 MONGOC_WRITE_CONCERN_W_MAJORITY
,用于集合。
mongoc_collection_t *collection = mongoc_client_get_collection (client, "<database name>", "<collection name>"); // Create a new write concern mongoc_write_concern_t *write_concern = mongoc_write_concern_new (); mongoc_write_concern_set_w (write_concern, MONGOC_WRITE_CONCERN_W_MAJORITY); // Set the write concern on the collection mongoc_collection_set_write_concern (collection, write_concern);
注意
集合和数据库是不可变的
mongoc_database_t
和 mongoc_collection_t
实例是不可变的。当你在数据库或集合上设置写入关注时,该方法返回一个新的实例,并不会影响原始实例。
有关写入关注的更多信息,请参阅写入关注,MongoDB 服务器手册中的内容。
读取关注
读取关注指定以下行为
副本集之间的因果一致性级别
在查询过程中维护的隔离保证
你可以通过调用以下函数来指定读取关注
mongoc_client_set_read_concern()
在客户端上设置读取关注。mongoc_transaction_opts_set_read_concern()
在事务上设置读取关注。mongoc_database_set_read_concern()
在数据库上设置读取关注。mongoc_collection_set_read_concern()
在集合上设置读取关注。
要指定读取关注的级别,请调用 mongoc_read_concern_set_level()
函数,并传入你的读取关注和以下值之一
MONGOC_READ_CONCERN_LEVEL_LOCAL
:查询返回实例的最新数据。不提供保证数据已写入大多数副本集成员。MONGOC_READ_CONCERN_LEVEL_AVAILABLE
:查询返回实例的最新数据。不提供保证数据已写入大多数副本集成员。使用因果一致性会话和事务时不可用ReadConcern.AVAILABLE
。MONGOC_READ_CONCERN_LEVEL_MAJORITY
:查询返回已由大多数副本集成员确认的数据。MONGOC_READ_CONCERN_LEVEL_LINEARIZABLE
:查询返回反映在读取操作开始之前完成的所有成功写入的数据。使用因果一致性会话和事务时不可用MONGOC_READ_CONCERN_LEVEL_LINEARIZABLE
。MONGOC_READ_CONCERN_LEVEL_SNAPSHOT
:查询返回来自最近过去特定单个点的跨分片的大多数已提交数据。
有关读取关注级别的更多信息,请参阅 MongoDB 服务器手册中的 读取关注级别。
以下示例将读取关注设置为 MONGOC_READ_CONCERN_LEVEL_MAJORITY
,用于 mongoc_client_t
实例。
mongoc_client_t *client = mongoc_client_new ("<connection string>"); // Create a new read concern mongoc_read_concern_t *read_concern = mongoc_read_concern_new (); // Set the read concern level mongoc_read_concern_set_level (read_concern, MONGOC_READ_CONCERN_LEVEL_MAJORITY); // Set the read concern on the client mongoc_client_set_read_concern (client, read_concern);
以下示例将读取关注设置为 MONGOC_READ_CONCERN_LEVEL_MAJORITY
,用于集合。
mongoc_collection_t *collection = mongoc_client_get_collection (client, "<database name>", "<collection name>"); // Create a new read concern mongoc_read_concern_t *read_concern = mongoc_read_concern_new (); // Set the read concern level mongoc_read_concern_set_level (read_concern, MONGOC_READ_CONCERN_LEVEL_MAJORITY); // Set the read concern on the collection mongoc_collection_set_read_concern (collection, read_concern);
要了解更多关于读取关注度的信息,请参阅MongoDB服务器手册中的读取关注。
读取偏好
读取偏好确定MongoDB在运行查询时从副本集的哪个成员读取。您可以通过以下函数设置读取偏好:
mongoc_client_set_read_prefs()
在客户端上设置读取偏好。mongoc_transaction_opts_set_read_prefs()
在事务上设置读取偏好。mongoc_database_set_read_prefs()
在数据库上设置读取偏好。mongoc_collection_set_read_prefs()
在集合上设置读取偏好。
要指定读取偏好的级别,调用mongoc_read_prefs_new()
函数,并传入以下值之一:
MONGOC_READ_PRIMARY
:查询从主节点返回数据。MONGOC_READ_PRIMARY_PREFERRED
:如果可用,查询从主节点返回数据。否则,查询从从节点返回数据。MONGOC_READ_SECONDARY
:查询从从节点返回数据。MONGOC_READ_SECONDARY_PREFERRED
:如果可用,查询从从节点返回数据。否则,查询从主节点返回数据。MONGOC_READ_NEAREST
:查询从具有最低网络延迟的节点返回数据。
以下示例将读取偏好设置为MONGOC_READ_SECONDARY
,用于mongoc_client_t
实例。
mongoc_client_t *client = mongoc_client_new ("<connection string>"); // Create a new read preference mongoc_read_prefs_t *read_prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY); // Set the read preference on the client mongoc_client_set_read_prefs (client, read_prefs);
以下示例将读取偏好设置为MONGOC_READ_SECONDARY
,用于集合。
mongoc_collection_t *collection = mongoc_client_get_collection (client, "<database name>", "<collection name>"); // Create a new read preference mongoc_read_prefs_t *read_prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY); // Set the read preference on the collection mongoc_collection_set_read_prefs (collection, read_prefs);
有关读取偏好的更多信息,请参阅MongoDB服务器手册中的读取偏好。
API 文档
要了解本指南中讨论的任何函数或类型,请参阅以下API文档: