1MONGOC_DATABASE_FIND_COLLECTIONS_WMIOTlNHiG_bOOmCPo_TnDSgA(oT3cA)BASE_FIND_COLLECTIONS_WITH_OPTS(3)
2
3
4
6 mongoc_cursor_t *
7 mongoc_database_find_collections_with_opts (mongoc_database_t *database,
8 const bson_t *opts)
9 BSON_GNUC_WARN_UNUSED_RESULT;
10
11 Fetches a cursor containing documents, each corresponding to a collec‐
12 tion on this database.
13
14 To get collection names only, use
15 mongoc_database_get_collection_names_with_opts().
16
17 This function is considered a retryable read operation. Upon a tran‐
18 sient error (a network error, errors due to replica set failover, etc.)
19 the operation is safely retried once. If retryreads is false in the
20 URI (see mongoc_uri_t) the retry behavior does not apply.
21
23 • database: A mongoc_database_t.
24
25 • opts: A bson_t containing additional options.
26
27 opts may be NULL or a BSON document with additional command options:
28
29 • sessionId: First, construct a mongoc_client_session_t with
30 mongoc_client_start_session(). You can begin a transaction with
31 mongoc_client_session_start_transaction(), optionally with a
32 mongoc_transaction_opt_t that overrides the options inherited from
33 database, and use mongoc_client_session_append() to add the session
34 to opts. See the example code for mongoc_client_session_t.
35
36 • serverId: To target a specific server, include an int32 "serverId"
37 field. Obtain the id by calling mongoc_client_select_server(), then
38 mongoc_server_description_id() on its return value.
39
40 For a list of all options, see the MongoDB Manual entry on the listCol‐
41 lections command.
42
44 Use mongoc_cursor_error() on the returned cursor to check for errors.
45
47 This function returns a newly allocated mongoc_cursor_t that should be
48 freed with mongoc_cursor_destroy() when no longer in use. The returned
49 mongoc_cursor_t is never NULL, even on error. The user must call
50 mongoc_cursor_next() on the returned mongoc_cursor_t to execute the
51 initial command.
52
53 Cursor errors can be checked with mongoc_cursor_error_document(). It
54 always fills out the bson_error_t if an error occurred, and optionally
55 includes a server reply document if the error occurred server-side.
56
57 WARNING:
58 Failure to handle the result of this function is a programming er‐
59 ror.
60
61 In the returned cursor each result corresponds to the server's repre‐
62 sentation of a collection in this database.
63
64 The cursor functions mongoc_cursor_set_limit(),
65 mongoc_cursor_set_batch_size(), and
66 mongoc_cursor_set_max_await_time_ms() have no use on the returned cur‐
67 sor.
68
70 {
71 bson_t opts = BSON_INITIALIZER;
72 bson_t name_filter;
73 const bson_t *doc;
74 bson_iter_t iter;
75 bson_error_t error;
76 mongoc_cursor_t *cursor;
77
78 BSON_APPEND_DOCUMENT_BEGIN (&opts, "filter", &name_filter);
79 /* find collections with names like "abbbbc" */
80 BSON_APPEND_REGEX (&name_filter, "name", "ab+c", NULL);
81 bson_append_document_end (&opts, &name_filter);
82
83 cursor = mongoc_database_find_collections_with_opts (database, &opts);
84 while (mongoc_cursor_next (cursor, &doc)) {
85 bson_iter_init_find (&iter, doc, "name");
86 printf ("found collection: %s\n", bson_iter_utf8 (&iter, NULL));
87 }
88
89 if (mongoc_cursor_error (cursor, &error)) {
90 fprintf (stderr, "%s\n", error.message);
91 }
92
93 mongoc_cursor_destroy (cursor);
94 bson_destroy (&opts);
95 }
96
97 SEE ALSO:
98 mongoc_database_get_collection_names_with_opts()
99
100
102 MongoDB, Inc
103
105 2017-present, MongoDB, Inc
106
107
108
109
1101.25.1 NMoOvNG0O8C,_D2A0T2A3BASE_FIND_COLLECTIONS_WITH_OPTS(3)