Skip to content

Commit 49106c8

Browse files
committed
CDRIVER-403 Allow count to work with query hints
Support an optional bson_t opt param for count which masses args through to the count command unchanged. This allows the user to pass arbitrary hints. Adds a new function to the abi
1 parent 048c851 commit 49106c8

File tree

7 files changed

+167
-0
lines changed

7 files changed

+167
-0
lines changed

build/cmake/libmongoc-ssl.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mongoc_collection_aggregate
4343
mongoc_collection_command
4444
mongoc_collection_command_simple
4545
mongoc_collection_count
46+
mongoc_collection_count_with_opts
4647
mongoc_collection_create_bulk_operation
4748
mongoc_collection_create_index
4849
mongoc_collection_delete

build/cmake/libmongoc.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mongoc_collection_aggregate
4343
mongoc_collection_command
4444
mongoc_collection_command_simple
4545
mongoc_collection_count
46+
mongoc_collection_count_with_opts
4647
mongoc_collection_create_bulk_operation
4748
mongoc_collection_create_index
4849
mongoc_collection_delete
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?xml version="1.0"?>
2+
3+
<page xmlns="http://projectmallard.org/1.0/"
4+
type="topic"
5+
style="function"
6+
xmlns:api="http://projectmallard.org/experimental/api/"
7+
xmlns:ui="http://projectmallard.org/experimental/ui/"
8+
id="mongoc_collection_count">
9+
10+
11+
<info>
12+
<link type="guide" xref="mongoc_collection_t" group="function"/>
13+
</info>
14+
<title>mongoc_collection_count()</title>
15+
16+
<section id="synopsis">
17+
<title>Synopsis</title>
18+
<synopsis><code mime="text/x-csrc"><![CDATA[int64_t
19+
mongoc_collection_count_with_opts (mongoc_collection_t *collection,
20+
mongoc_query_flags_t flags,
21+
const bson_t *query,
22+
int64_t skip,
23+
int64_t limit,
24+
const bson_t *opts,
25+
const mongoc_read_prefs_t *read_prefs,
26+
bson_error_t *error);
27+
]]></code></synopsis>
28+
</section>
29+
30+
<section id="parameters">
31+
<title>Parameters</title>
32+
<table>
33+
<tr><td><p>collection</p></td><td><p>A <link xref="mongoc_collection_t">mongoc_collection_t</link>.</p></td></tr>
34+
<tr><td><p>flags</p></td><td><p>A <link xref="mongoc_query_flags_t">mongoc_query_flags_t</link>.</p></td></tr>
35+
<tr><td><p>query</p></td><td><p>A bson_t containing the query.</p></td></tr>
36+
<tr><td><p>skip</p></td><td><p>A int64_t, zero to ignore.</p></td></tr>
37+
<tr><td><p>limit</p></td><td><p>A int64_t, zero to ignore.</p></td></tr>
38+
<tr><td><p>opts</p></td><td><p>A bson_t, NULL to ignore.</p></td></tr>
39+
<tr><td><p>read_prefs</p></td><td><p>A <link xref="mongoc_read_prefs_t">mongoc_read_prefs_t</link> or NULL.</p></td></tr>
40+
<tr><td><p>error</p></td><td><p>An optional location for a <link xref="bson_error_t">bson_error_t</link> or <code>NULL</code>.</p></td></tr>
41+
</table>
42+
</section>
43+
44+
<section id="description">
45+
<title>Description</title>
46+
<p>This function shall execute a count query on the underlying 'collection'. The bson 'query' is not validated, simply passed along as appropriate to the server. As such, compatibility and errors should be validated in the appropriate server documentation.</p>
47+
<p>In addition to the standard functionality available from mongoc_collection_count, this function allows the user to add arbitrary extra keys to the count. This pass through enables features such as hinting for counts.</p>
48+
<p>For more information, see the <link href="http://docs.mongodb.org/manual/reference/operator/query/">query reference</link> at the MongoDB website.</p>
49+
</section>
50+
51+
<section id="errors">
52+
<title>Errors</title>
53+
<p>Errors are propagated via the <code>error</code> parameter.</p>
54+
</section>
55+
56+
<section id="return">
57+
<title>Returns</title>
58+
<p>-1 on failure, otherwise the number of documents counted.</p>
59+
</section>
60+
61+
<section id="example">
62+
<title>Example</title>
63+
<listing>
64+
<title>Count Example</title>
65+
<code mime="text/x-csrc"><![CDATA[#include <mongoc.h>
66+
#include <bcon.h>
67+
#include <stdio.h>
68+
69+
static void
70+
print_query_count (mongoc_collection_t *collection,
71+
bson_t *query)
72+
{
73+
bson_error_t error;
74+
int64_t count;
75+
bson_t opts;
76+
77+
bson_init(&opts);
78+
BSON_APPEND_UTF8(&opts, "hint", "_id_");
79+
80+
count = mongoc_collection_count (collection, MONGOC_QUERY_NONE, query, 0, 0, &opts, NULL, &error);
81+
82+
bson_destroy(&opts);
83+
84+
if (count < 0) {
85+
fprintf (stderr, "Count failed: %s\n", error.message);
86+
} else {
87+
printf ("%"PRId64" documents counted.\n", count);
88+
}
89+
}]]></code>
90+
</listing>
91+
</section>
92+
93+
</page>

src/libmongoc.symbols

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ mongoc_collection_aggregate
4444
mongoc_collection_command
4545
mongoc_collection_command_simple
4646
mongoc_collection_count
47+
mongoc_collection_count_with_opts
4748
mongoc_collection_create_bulk_operation
4849
mongoc_collection_create_index
4950
mongoc_collection_delete

src/mongoc/mongoc-collection.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,28 @@ mongoc_collection_count (mongoc_collection_t *collection, /* IN */
532532
int64_t limit, /* IN */
533533
const mongoc_read_prefs_t *read_prefs, /* IN */
534534
bson_error_t *error) /* OUT */
535+
{
536+
return mongoc_collection_count_with_opts (
537+
collection,
538+
flags,
539+
query,
540+
skip,
541+
limit,
542+
NULL,
543+
read_prefs,
544+
error);
545+
}
546+
547+
548+
int64_t
549+
mongoc_collection_count_with_opts (mongoc_collection_t *collection, /* IN */
550+
mongoc_query_flags_t flags, /* IN */
551+
const bson_t *query, /* IN */
552+
int64_t skip, /* IN */
553+
int64_t limit, /* IN */
554+
const bson_t *opts, /* IN */
555+
const mongoc_read_prefs_t *read_prefs, /* IN */
556+
bson_error_t *error) /* OUT */
535557
{
536558
int64_t ret = -1;
537559
bson_iter_t iter;
@@ -557,6 +579,9 @@ mongoc_collection_count (mongoc_collection_t *collection, /* IN */
557579
if (skip) {
558580
bson_append_int64(&cmd, "skip", 4, skip);
559581
}
582+
if (opts) {
583+
bson_concat(&cmd, opts);
584+
}
560585
if (mongoc_collection_command_simple(collection, &cmd, read_prefs,
561586
&reply, error) &&
562587
bson_iter_init_find(&iter, &reply, "n")) {

src/mongoc/mongoc-collection.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ int64_t mongoc_collection_count (mongoc_col
6666
int64_t limit,
6767
const mongoc_read_prefs_t *read_prefs,
6868
bson_error_t *error);
69+
int64_t mongoc_collection_count_with_opts (mongoc_collection_t *collection,
70+
mongoc_query_flags_t flags,
71+
const bson_t *query,
72+
int64_t skip,
73+
int64_t limit,
74+
const bson_t *opts,
75+
const mongoc_read_prefs_t *read_prefs,
76+
bson_error_t *error);
6977
bool mongoc_collection_drop (mongoc_collection_t *collection,
7078
bson_error_t *error);
7179
bool mongoc_collection_drop_index (mongoc_collection_t *collection,

tests/test-mongoc-collection.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,43 @@ test_count (void)
537537
}
538538

539539

540+
static void
541+
test_count_with_opts (void)
542+
{
543+
mongoc_collection_t *collection;
544+
mongoc_client_t *client;
545+
bson_error_t error;
546+
int64_t count;
547+
bson_t b;
548+
bson_t opts;
549+
550+
client = mongoc_client_new (gTestUri);
551+
ASSERT (client);
552+
553+
collection = mongoc_client_get_collection (client, "test", "test");
554+
ASSERT (collection);
555+
556+
bson_init (&opts);
557+
558+
BSON_APPEND_UTF8 (&opts, "hint", "_id_");
559+
560+
bson_init (&b);
561+
count = mongoc_collection_count_with_opts (collection, MONGOC_QUERY_NONE, &b,
562+
0, 0, &opts, NULL, &error);
563+
bson_destroy (&b);
564+
bson_destroy (&opts);
565+
566+
if (count == -1) {
567+
MONGOC_WARNING ("%s\n", error.message);
568+
}
569+
570+
ASSERT (count != -1);
571+
572+
mongoc_collection_destroy (collection);
573+
mongoc_client_destroy (client);
574+
}
575+
576+
540577
static void
541578
test_drop (void)
542579
{
@@ -1183,6 +1220,7 @@ test_collection_install (TestSuite *suite)
11831220
TestSuite_Add (suite, "/Collection/update", test_update);
11841221
TestSuite_Add (suite, "/Collection/remove", test_remove);
11851222
TestSuite_Add (suite, "/Collection/count", test_count);
1223+
TestSuite_Add (suite, "/Collection/count_with_opts", test_count_with_opts);
11861224
TestSuite_Add (suite, "/Collection/drop", test_drop);
11871225
TestSuite_Add (suite, "/Collection/aggregate", test_aggregate);
11881226
TestSuite_Add (suite, "/Collection/validate", test_validate);

0 commit comments

Comments
 (0)