Skip to content

Commit 830d849

Browse files
author
Christian Hergert
committed
collection: allow fully qualified ns name using collection name.
this way a collection called $cmd.foo.bar can work.
1 parent 7eb29cc commit 830d849

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/mongoc/mongoc-collection.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ mongoc_collection_command (mongoc_collection_t *collection,
462462
const bson_t *fields,
463463
const mongoc_read_prefs_t *read_prefs)
464464
{
465+
char ns[MONGOC_NAMESPACE_MAX];
466+
465467
BSON_ASSERT (collection);
466468
BSON_ASSERT (query);
467469

@@ -471,7 +473,14 @@ mongoc_collection_command (mongoc_collection_t *collection,
471473

472474
bson_clear (&collection->gle);
473475

474-
return mongoc_client_command (collection->client, collection->db, flags,
476+
if (NULL == strstr (collection->collection, "$cmd")) {
477+
bson_snprintf (ns, sizeof ns, "%s", collection->db);
478+
} else {
479+
bson_snprintf (ns, sizeof ns, "%s.%s",
480+
collection->db, collection->collection);
481+
}
482+
483+
return mongoc_client_command (collection->client, ns, flags,
475484
skip, limit, batch_size, query, fields, read_prefs);
476485
}
477486

tests/test-mongoc-collection.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,38 @@ END_IGNORE_DEPRECATIONS;
986986
}
987987

988988

989+
static void
990+
test_command_fq (void)
991+
{
992+
mongoc_collection_t *collection;
993+
mongoc_client_t *client;
994+
mongoc_cursor_t *cursor;
995+
const bson_t *doc = NULL;
996+
bson_t *cmd;
997+
bool r;
998+
999+
client = mongoc_client_new (gTestUri);
1000+
ASSERT (client);
1001+
1002+
collection = get_test_collection (client, "$cmd.sys.inprog");
1003+
ASSERT (collection);
1004+
1005+
cmd = BCON_NEW ("query", "{", "}");
1006+
1007+
cursor = mongoc_collection_command (collection, MONGOC_QUERY_NONE, 0, 1, 0, cmd, NULL, NULL);
1008+
r = mongoc_cursor_next (cursor, &doc);
1009+
assert (r);
1010+
1011+
r = mongoc_cursor_next (cursor, &doc);
1012+
assert (!r);
1013+
1014+
mongoc_cursor_destroy (cursor);
1015+
bson_destroy (cmd);
1016+
mongoc_collection_destroy (collection);
1017+
mongoc_client_destroy (client);
1018+
}
1019+
1020+
9891021
static void
9901022
cleanup_globals (void)
9911023
{
@@ -1014,6 +1046,7 @@ test_collection_install (TestSuite *suite)
10141046
TestSuite_Add (suite, "/Collection/find_and_modify", test_find_and_modify);
10151047
TestSuite_Add (suite, "/Collection/large_return", test_large_return);
10161048
TestSuite_Add (suite, "/Collection/many_return", test_many_return);
1049+
TestSuite_Add (suite, "/Collection/command_fully_qualified", test_command_fq);
10171050

10181051
atexit (cleanup_globals);
10191052
}

0 commit comments

Comments
 (0)