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

NAME

6       mongoc_read_prefs_set_tags - mongoc_read_prefs_set_tags()
7

SYNOPSIS

9          void
10          mongoc_read_prefs_set_tags (mongoc_read_prefs_t *read_prefs,
11                                      const bson_t *tags);
12

PARAMETERS

14       · read_prefs: A mongoc_read_prefs_t.
15
16       · tags: A bson_t.
17

DESCRIPTION

19       Sets the tags to be used for the read preference. Only mongod instances
20       matching these tags will be suitable for handling the request.
21

EXAMPLES

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

AUTHOR

89       MongoDB, Inc
90
92       2017-present, MongoDB, Inc
93
94
95
96
971.16.2                           Feb 25, 2020    MONGOC_READ_PREFS_SET_TAGS(3)
Impressum