|
7 | 7 | #include <stdio.h>
|
8 | 8 | #include <stdlib.h>
|
9 | 9 |
|
10 |
| -static void |
11 |
| -command_started (const mongoc_apm_command_started_t *event) |
| 10 | +int |
| 11 | +main (int argc, char *argv[]) |
12 | 12 | {
|
13 |
| - char *s; |
14 |
| - |
15 |
| - s = bson_as_relaxed_extended_json ( |
16 |
| - mongoc_apm_command_started_get_command (event), NULL); |
17 |
| - printf ("Command %s started on %s:\n%s\n\n", |
18 |
| - mongoc_apm_command_started_get_command_name (event), |
19 |
| - mongoc_apm_command_started_get_host (event)->host, |
20 |
| - s); |
21 |
| - |
22 |
| - bson_free (s); |
23 |
| -} |
24 |
| - |
25 |
| -static void example_listcollections (mongoc_client_t *client) { |
26 |
| - mongoc_database_t *db; |
| 13 | + mongoc_client_t *client; |
| 14 | + mongoc_collection_t *collection; |
27 | 15 | mongoc_cursor_t *cursor;
|
| 16 | + bson_error_t error; |
28 | 17 | const bson_t *doc;
|
29 |
| - bson_t* opts = BCON_NEW ("cursor", "{", "batchSize", BCON_INT32(1), "}"); |
| 18 | + const char *collection_name = "test"; |
| 19 | + bson_t query; |
30 | 20 | char *str;
|
31 |
| - bson_error_t error; |
| 21 | + const char *uri_string = "mongodb://127.0.0.1/?appname=client-example"; |
| 22 | + mongoc_uri_t *uri; |
32 | 23 |
|
33 |
| - MONGOC_DEBUG ("Example listCollections"); |
34 |
| - MONGOC_DEBUG ("listCollections with the option {cursor: {batchSize: 1}}"); |
35 |
| - db = mongoc_client_get_database(client, "test"); |
36 |
| - cursor = mongoc_database_find_collections_with_opts (db, opts); |
| 24 | + mongoc_init (); |
| 25 | + if (argc > 1) { |
| 26 | + uri_string = argv[1]; |
| 27 | + } |
37 | 28 |
|
38 |
| - MONGOC_DEBUG ("listCollections results:"); |
39 |
| - while (mongoc_cursor_next (cursor, &doc)) { |
40 |
| - str = bson_as_canonical_extended_json (doc, NULL); |
41 |
| - fprintf (stdout, "- %s\n", str); |
42 |
| - bson_free (str); |
| 29 | + if (argc > 2) { |
| 30 | + collection_name = argv[2]; |
43 | 31 | }
|
44 | 32 |
|
45 |
| - if (mongoc_cursor_error (cursor, &error)) { |
46 |
| - fprintf (stderr, "listCollections error: %s\n", error.message); |
47 |
| - return; |
| 33 | + uri = mongoc_uri_new_with_error (uri_string, &error); |
| 34 | + if (!uri) { |
| 35 | + fprintf (stderr, |
| 36 | + "failed to parse URI: %s\n" |
| 37 | + "error message: %s\n", |
| 38 | + uri_string, |
| 39 | + error.message); |
| 40 | + return EXIT_FAILURE; |
48 | 41 | }
|
49 |
| - mongoc_cursor_destroy (cursor); |
50 |
| - mongoc_database_destroy (db); |
51 |
| - bson_destroy (opts); |
52 |
| -} |
53 | 42 |
|
54 |
| -static void example_listindexes (mongoc_client_t *client) { |
55 |
| - mongoc_collection_t *coll; |
56 |
| - mongoc_cursor_t *cursor; |
57 |
| - const bson_t *doc; |
58 |
| - bson_t* opts = BCON_NEW ("cursor", "{", "batchSize", BCON_INT32(1), "}"); |
59 |
| - char *str; |
60 |
| - bson_error_t error; |
| 43 | + client = mongoc_client_new_from_uri (uri); |
| 44 | + if (!client) { |
| 45 | + return EXIT_FAILURE; |
| 46 | + } |
61 | 47 |
|
62 |
| - MONGOC_DEBUG ("Example listIndexes"); |
63 |
| - MONGOC_DEBUG ("listIndexes with the option {cursor: {batchSize: 1}}"); |
64 |
| - coll = mongoc_client_get_collection(client, "test", "test"); |
65 |
| - cursor = mongoc_collection_find_indexes_with_opts (coll, opts); |
| 48 | + mongoc_client_set_error_api (client, 2); |
| 49 | + |
| 50 | + bson_init (&query); |
| 51 | + collection = mongoc_client_get_collection (client, "test", collection_name); |
| 52 | + cursor = mongoc_collection_find_with_opts ( |
| 53 | + collection, |
| 54 | + &query, |
| 55 | + NULL, /* additional options */ |
| 56 | + NULL); /* read prefs, NULL for default */ |
66 | 57 |
|
67 |
| - MONGOC_DEBUG ("listIndexes results:"); |
68 | 58 | while (mongoc_cursor_next (cursor, &doc)) {
|
69 | 59 | str = bson_as_canonical_extended_json (doc, NULL);
|
70 |
| - fprintf (stdout, "- %s\n", str); |
| 60 | + fprintf (stdout, "%s\n", str); |
71 | 61 | bson_free (str);
|
72 | 62 | }
|
73 | 63 |
|
74 | 64 | if (mongoc_cursor_error (cursor, &error)) {
|
75 |
| - fprintf (stderr, "listCollections error: %s\n", error.message); |
76 |
| - return; |
77 |
| - } |
78 |
| - mongoc_cursor_destroy (cursor); |
79 |
| - mongoc_collection_destroy (coll); |
80 |
| - bson_destroy (opts); |
81 |
| -} |
82 |
| - |
83 |
| -int |
84 |
| -main (int argc, char *argv[]) |
85 |
| -{ |
86 |
| - mongoc_client_t *client; |
87 |
| - const char *uri_string = "mongodb://127.0.0.1/?appname=client-example"; |
88 |
| - mongoc_apm_callbacks_t *callbacks; |
89 |
| - |
90 |
| - mongoc_init (); |
91 |
| - if (argc > 1) { |
92 |
| - uri_string = argv[1]; |
93 |
| - } |
94 |
| - |
95 |
| - client = mongoc_client_new (uri_string); |
96 |
| - if (!client) { |
| 65 | + fprintf (stderr, "Cursor Failure: %s\n", error.message); |
97 | 66 | return EXIT_FAILURE;
|
98 | 67 | }
|
99 | 68 |
|
100 |
| - callbacks = mongoc_apm_callbacks_new (); |
101 |
| - mongoc_apm_set_command_started_cb (callbacks, command_started); |
102 |
| - mongoc_client_set_apm_callbacks (client, callbacks, NULL); |
103 |
| - mongoc_client_set_error_api (client, 2); |
104 |
| - |
105 |
| - // example_listcollections (client); |
106 |
| - example_listindexes (client); |
107 |
| - |
| 69 | + bson_destroy (&query); |
| 70 | + mongoc_cursor_destroy (cursor); |
| 71 | + mongoc_collection_destroy (collection); |
| 72 | + mongoc_uri_destroy (uri); |
108 | 73 | mongoc_client_destroy (client);
|
109 | 74 | mongoc_cleanup ();
|
110 | 75 |
|
|
0 commit comments