Docs 菜单
Docs 主页
/ /

配置增删改查操作

在本指南中,您可以了解如何使用写关注(write concern)读关注(read concern)读取偏好(read preference)配置来修改 MongoDB 对副本集运行创建、读取、更新和删除 (CRUD) 操作的方式。

您可以在以下级别设立这些配置:

  1. 客户端,为所有操作执行设置默认,除非被覆盖

  2. 事务

  3. Database

  4. Collection

前面的列表按优先级递增。 示例,如果您在客户端和数据库级别都设立了读读关注(read concern),则在数据库级别指定的读关注将覆盖在客户端级别指定的读关注(read concern)。

写关注指定在操作成功返回之前从MongoDB请求的写入操作确认级别。 未指定显式写关注(write concern)的操作会继承全局默认写关注(write concern)设置。

您可以通过调用以下函数设立写关注(write concern):

  • mongoc_client_set_write_concern() 在客户端上设置写关注(write concern)。

  • mongoc_transaction_opts_set_write_concern() 在ACID 事务上设置写关注(write concern)。

  • mongoc_database_set_write_concern() 在数据库上设置写关注(write concern)。

  • mongoc_collection_set_write_concern() 在集合上设置写关注(write concern)。

要指定写关注(write concern)级别,请调用 mongoc_write_concern_set_w() 函数,并传入写关注(write concern)和以下值之一:

  • MONGOC_WRITE_CONCERN_W_DEFAULT:写入操作在写入内存后返回。

  • 0:主节点 (primary node in the replica set)节点处理完写入操作后,写入操作将返回。

  • 1:写入操作仅在主节点 (primary node in the replica set)节点确认写入操作后返回,而无需等待从从节点(secondary node from replica set)确认。

  • MONGOC_WRITE_CONCERN_W_MAJORITY:在大多数副本集成员确认写入操作后,写入操作将返回。

  • MONGOC_WRITE_CONCERN_W_TAG:在具有指定标签的副本集节点确认写入操作后,写入操作返回。

以下示例将 实例的写关注(write concern)设置为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);

以下示例将集合的写关注(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_tmongoc_collection_t实例是不可变的。 在数据库或集合上设立写关注(write concern)时,该方法会返回一个新实例,不会影响原始实例。

有关写关注(write concern)的更多信息,请参阅MongoDB Server手册中的写关注

读关注指定以下行为:

您可以通过调用以下函数来指定读关注(read concern):

  • mongoc_client_set_read_concern() 在客户端上设置读关注(read concern)。

  • mongoc_transaction_opts_set_read_concern() 在ACID 事务上设置读关注(read concern)。

  • mongoc_database_set_read_concern() 在数据库上设置读关注(read concern)。

  • mongoc_collection_set_read_concern() 在集合上设置读关注(read concern)。

要指定读关注(read concern)级别,请调用 mongoc_read_concern_set_level() 函数,并传入读关注(read concern)和以下值之一:

  • 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:查询返回从最近的特定单点开始跨分片出现的多数提交数据。

有关读关注(read concern)级别的更多信息,请参阅MongoDB Server手册中的读关注级别

以下示例将 实例的读关注(readMONGOC_READ_CONCERN_LEVEL_MAJORITY concern)设置为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);

以下示例将集合的读关注(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);

要学习;了解有关读关注(read concern)的更多信息,请参阅MongoDB Server手册中的读关注

读取偏好决定了MongoDB在运行查询时会读取副本集的哪个成员。 您可以通过调用以下函数设立读取偏好(read preference):

  • mongoc_client_set_read_prefs() 设置客户端的读取偏好(read preference)。

  • mongoc_transaction_opts_set_read_prefs() 设置ACID 事务的读取偏好(read preference)。

  • mongoc_database_set_read_prefs() 设置数据库的读取偏好(read preference)。

  • mongoc_collection_set_read_prefs() 设置集合的读取偏好(read preference)。

要指定读取偏好(read preference)的级别,请调用 mongoc_read_prefs_new() 函数,并传入以下值之一:

  • MONGOC_READ_PRIMARY:查询从主节点 (primary node in the replica set)节点返回数据。

  • MONGOC_READ_PRIMARY_PREFERRED:查询会从主节点 (primary node in the replica set)节点返回数据(如果可用)。 否则,查询将从从节点(secondary node from replica set)节点返回数据。

  • MONGOC_READ_SECONDARY:查询节点从节点(secondary node from replica set)数据。

  • MONGOC_READ_SECONDARY_PREFERRED:查询从节点(如果可用)返回查询,主节点 (primary node in the replica set)从节点(secondary node from replica set)节点数据。

  • MONGOC_READ_NEAREST:查询会从网络延迟最低的节点返回数据。

以下示例将 的实例的读取偏好(readMONGOC_READ_SECONDARY preference)设置为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);

以下示例将集合的读取偏好(read preference)设置为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);

有关读取偏好(read preference)的更多信息,请参阅MongoDB Server手册中的读取偏好

对集合执行写入操作时,可以指定排序规则。

排序规则是一设立特定于语言的字符串比较规则,例如字母大小写和重音符号规则。

要指定排序规则,请将排序规则定义传递给读取或写入操作选项中的 "collation" 键。

考虑包含以下文档的集合:

{ _id: 1, category: "café" },
{ _id: 2, category: "cafe" },
{ _id: 3, category: "cafE" }

以下示例创建了一个排序规则,该排序规则指定法语区域设置设置并忽略大小写和字母变体之间的差异。然后,它使用此排序规则来查找 category字段值与 "cafe" 匹配的文档。由于指定的排序规则,查询将返回所有三个文档。

mongoc_collection_t *collection = mongoc_client_get_collection(client, "<database name>", "<collection name>");
bson_t *opts = BCON_NEW(
"collation", "{",
"locale", BCON_UTF8("fr"),
"strength", BCON_INT32(1),
"}"
);
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(
collection,
BCON_NEW("category", BCON_UTF8("cafe")),
opts,
NULL
);
const bson_t *doc;
while (mongoc_cursor_next(cursor, &doc))
{
char *str = bson_as_canonical_extended_json(doc, NULL);
printf("%s\n", str);
bson_free(str);
}
{ "_id": { "$oid" : "1" }, "category": "café" }
{ "_id": { "$oid" : "2" }, "category": "cafe" }
{ "_id": { "$oid" : "3" }, "category": "cafE" }

要学习;了解有关排序规则的更多信息,请参阅MongoDB Server手册中的排序规则参考文档。

要学习;了解有关本指南中讨论的任何函数或类型的更多信息,请参阅以下API文档:

后退

事务

在此页面上