Skip to content

Commit ee78062

Browse files
committed
check that $listSearchIndexes is supported
1 parent 6b514af commit ee78062

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/libmongoc/examples/example-manage-search-indexes.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,34 @@ main (int argc, char *argv[])
8282
mongoc_database_destroy (db);
8383
}
8484

85+
// Check that $listSearchIndexes pipeline stage is supported.
86+
// This is intended to check that a MongoDB Atlas cluster is used.
87+
{
88+
const char *pipeline_str =
89+
BSON_STR ({"pipeline" : [ {"$listSearchIndexes" : {}} ]});
90+
bson_t pipeline;
91+
ASSERT (bson_init_from_json (&pipeline, pipeline_str, -1, &error));
92+
mongoc_cursor_t *cursor =
93+
mongoc_collection_aggregate (coll,
94+
MONGOC_QUERY_NONE,
95+
&pipeline,
96+
NULL /* opts */,
97+
NULL /* read_prefs */);
98+
const bson_t *got;
99+
while (mongoc_cursor_next (cursor, &got))
100+
;
101+
if (mongoc_cursor_error (cursor, &error)) {
102+
bson_destroy (&pipeline);
103+
mongoc_cursor_destroy (cursor);
104+
HANDLE_ERROR ("Failed to run $listSearchIndexes with error: %s\n"
105+
"Does the URI point to a MongoDB Atlas cluster? %s",
106+
error.message,
107+
uri_string);
108+
}
109+
bson_destroy (&pipeline);
110+
mongoc_cursor_destroy (cursor);
111+
}
112+
85113
{
86114
// Create an Atlas Search Index ... begin
87115
bson_t cmd;
@@ -102,10 +130,7 @@ main (int argc, char *argv[])
102130
if (!mongoc_collection_command_simple (
103131
coll, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) {
104132
bson_destroy (&cmd);
105-
HANDLE_ERROR ("Failed to run createSearchIndexes: %s.\n"
106-
"The URI '%s' must refer to a MongoDB Atlas cluster.",
107-
error.message,
108-
uri_string);
133+
HANDLE_ERROR ("Failed to run createSearchIndexes: %s", error.message);
109134
}
110135
printf ("Created index: \"test-index\"\n");
111136
bson_destroy (&cmd);

0 commit comments

Comments
 (0)