-
Notifications
You must be signed in to change notification settings - Fork 2
/
list_collections.c
79 lines (67 loc) · 1.98 KB
/
list_collections.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// list_collections.c
#include "URI.h"
#include <mongoc/mongoc.h>
int main(int argc, char const *argv[]) {
// your MongoDB URI connection string
const char *uri_string = MY_MONGODB_URI;
// MongoDB URI created from above string
mongoc_uri_t *uri;
// MongoDB Client, used to connect to the DB
mongoc_client_t *client;
// Error management
bson_error_t error;
mongoc_database_t *database;
mongoc_collection_t *collection;
char **collection_names;
unsigned i;
/*
* Required to initialize libmongoc's internals
*/
mongoc_init();
/*
* Safely create a MongoDB URI object from the given string
*/
uri = mongoc_uri_new_with_error(uri_string, &error);
if (!uri) {
fprintf(stderr,
"failed to parse URI: %s\n"
"error message: %s\n",
uri_string, error.message);
return EXIT_FAILURE;
}
/*
* Create a new client instance, here we use the uri we just built
*/
client = mongoc_client_new_from_uri(uri);
if (!client) {
return EXIT_FAILURE;
}
/*
* Register the application name so we can track it in the profile logs
* on the server. This can also be done from the URI (see other examples).
*/
mongoc_client_set_appname(client, "connect-example");
/*
* Get a handle on the database "db_name" and collection "coll_name"
*/
database = mongoc_client_get_database(client, "sample_mflix");
// getting all collection names, here we're not passing in any options
if ((collection_names = mongoc_database_get_collection_names_with_opts(
database, NULL, &error))) {
for (i = 0; collection_names[i]; i++) {
printf("%s\n", collection_names[i]);
}
bson_strfreev (collection_names);
} else {
fprintf(stderr, "Error: %s\n", error.message);
return EXIT_FAILURE;
}
/*
* Release our handles and clean up libmongoc
*/
mongoc_database_destroy(database);
mongoc_uri_destroy(uri);
mongoc_client_destroy(client);
mongoc_cleanup();
return EXIT_SUCCESS;
}