1MONGOC_READ_PREFS_SET_TAGS(3)      libmongoc     MONGOC_READ_PREFS_SET_TAGS(3)
2
3
4

SYNOPSIS

6          void
7          mongoc_read_prefs_set_tags (mongoc_read_prefs_t *read_prefs,
8                                      const bson_t *tags);
9

PARAMETERS

11read_prefs: A mongoc_read_prefs_t.
12
13tags: A bson_t.
14

DESCRIPTION

16       Sets the tags to be used for the read preference. Only mongod instances
17       matching these tags will be suitable for handling the request.
18

EXAMPLES

20          #include <mongoc/mongoc.h>
21
22          static void
23          run_query_with_read_prefs_tags (mongoc_collection_t *collection)
24          {
25             char *str;
26             const bson_t *doc;
27             bson_t filter = BSON_INITIALIZER;
28             bson_error_t error;
29             mongoc_cursor_t *cursor;
30             mongoc_read_prefs_t *read_prefs;
31             /*  Create a tagset representing
32              *  [
33              *    {"dc": "ny", "rack": "1" }, // Any node in rack1 in the ny datacenter
34              *    {"dc": "ny", "rack": "2" }, // Any node in rack2 in the ny datacenter
35              *    {"dc": "ny" },              // Any node in the ny datacenter
36              *    {}                          // If all else fails, just any available node
37              * ]
38              */
39             bson_t *tags = BCON_NEW (
40                "0", "{", "dc", BCON_UTF8("ny"), "rack", BCON_UTF8("1"), "}",
41                "1", "{", "dc", BCON_UTF8("ny"), "rack", BCON_UTF8("2"), "}",
42                "2", "{", "dc", BCON_UTF8("ny"), "}",
43                "3", "{", "}"
44             );
45
46             read_prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY);
47             mongoc_read_prefs_set_tags (read_prefs, tags);
48             bson_destroy (tags);
49
50             cursor =
51                mongoc_collection_find_with_opts (collection, &filter, NULL, read_prefs);
52
53             while (mongoc_cursor_next (cursor, &doc)) {
54                str = bson_as_canonical_extended_json (doc, NULL);
55                printf ("%s\n", str);
56                bson_free (str);
57             }
58             if (mongoc_cursor_error (cursor, &error)) {
59                fprintf (stderr, "Cursor error: %s\n", error.message);
60             }
61
62             mongoc_cursor_destroy (cursor);
63             mongoc_read_prefs_destroy (read_prefs);
64             bson_destroy (doc);
65          }
66
67          int main (void)
68          {
69             mongoc_client_t *client;
70             mongoc_collection_t *collection;
71
72             mongoc_init ();
73
74             client =
75                mongoc_client_new ("mongodb://localhost/?appname=rp_tags&replicaSet=foo");
76             mongoc_client_set_error_api (client, 2);
77             collection = mongoc_client_get_collection (client, "dbname", "collname");
78             run_query_with_read_prefs_tags (collection);
79
80             mongoc_collection_destroy (collection);
81             mongoc_client_destroy (client);
82             mongoc_cleanup();
83          }
84

AUTHOR

86       MongoDB, Inc
87
89       2017-present, MongoDB, Inc
90
91
92
93
941.25.1                           Nov 08, 2023    MONGOC_READ_PREFS_SET_TAGS(3)
Impressum