Skip to content

Commit aa246ec

Browse files
author
Christian Hergert
committed
examples: add aggregation example with compile time checks.
Now we can just include it in the generated docs and ensure it compiles correctly like the others.
1 parent 7f45e51 commit aa246ec

File tree

4 files changed

+61
-29
lines changed

4 files changed

+61
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*~
22
*.a
33
aclocal.m4
4+
aggregation1
45
AUTHORS
56
autom4te.cache
67
bulk1

doc/aggregate.page

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -62,35 +62,7 @@ connecting to: test</output>
6262
<section>
6363
<title>States with Populations Over 10 Million</title>
6464
<p>To get all states with a population greater than 10 million, use the following aggregation pipeline:</p>
65-
<synopsis><code mime="text/x-csrc"><![CDATA[#include <mongoc.h>
66-
#include <bcon.h>
67-
#include <stdio.h>
68-
69-
static void
70-
print_pipeline (mongoc_collection_t *collection)
71-
{
72-
bson_t *pipeline;
73-
mongoc_cursor_t *cursor;
74-
const bson_t *doc;
75-
76-
pipeline = BCON_NEW ("pipeline", "[",
77-
"{", "$group", "{", "_id", "$state", "total_pop", "{", "$sum", "$pop", "}", "}", "}",
78-
"{", "$match", "{", "total_pop", "{", "$gte", BCON_INT32 (10000000), "}", "}", "}",
79-
"]");
80-
81-
cursor = mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
82-
83-
while (mongoc_cursor_next (cursor, &doc)) {
84-
char *str;
85-
86-
str = bson_as_json (doc, NULL);
87-
printf ("%s\n", str);
88-
bson_free (str);
89-
}
90-
91-
mongoc_cursor_destroy (cursor);
92-
bson_destroy (pipeline);
93-
}]]></code></synopsis>
65+
<synopsis><code mime="text/x-csrc"><include parse="text" href="../examples/aggregation/aggregation1.c" xmlns="http://www.w3.org/2001/XInclude" /></code></synopsis>
9466

9567
<p>You should see a result like the following:</p>
9668

examples/Makefile.am

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,8 @@ noinst_PROGRAMS += bulk3
6868
bulk3_SOURCES = examples/bulk/bulk3.c
6969
bulk3_CFLAGS = $(EXAMPLE_CFLAGS)
7070
bulk3_LDADD = $(EXAMPLE_LDADD)
71+
72+
noinst_PROGRAMS += aggregation1
73+
aggregation1_SOURCES = examples/aggregation/aggregation1.c
74+
aggregation1_CFLAGS = $(EXAMPLE_CFLAGS)
75+
aggregation1_LDADD = $(EXAMPLE_LDADD)

examples/aggregation/aggregation1.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <mongoc.h>
2+
#include <stdio.h>
3+
4+
static void
5+
print_pipeline (mongoc_collection_t *collection)
6+
{
7+
mongoc_cursor_t *cursor;
8+
bson_error_t error;
9+
const bson_t *doc;
10+
bson_t *pipeline;
11+
char *str;
12+
13+
pipeline = BCON_NEW ("pipeline", "[",
14+
"{", "$group", "{", "_id", "$state", "total_pop", "{", "$sum", "$pop", "}", "}", "}",
15+
"{", "$match", "{", "total_pop", "{", "$gte", BCON_INT32 (10000000), "}", "}", "}",
16+
"]");
17+
18+
cursor = mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
19+
20+
while (mongoc_cursor_next (cursor, &doc)) {
21+
str = bson_as_json (doc, NULL);
22+
printf ("%s\n", str);
23+
bson_free (str);
24+
}
25+
26+
if (mongoc_cursor_error (cursor, &error)) {
27+
fprintf (stderr, "Cursor Failure: %s\n", error.message);
28+
}
29+
30+
mongoc_cursor_destroy (cursor);
31+
bson_destroy (pipeline);
32+
}
33+
34+
int
35+
main (int argc,
36+
char *argv[])
37+
{
38+
mongoc_client_t *client;
39+
mongoc_collection_t *collection;
40+
41+
mongoc_init ();
42+
43+
client = mongoc_client_new ("mongodb://localhost:27017");
44+
collection = mongoc_client_get_collection (client, "test", "zipcodes");
45+
46+
print_pipeline (collection);
47+
48+
mongoc_collection_destroy (collection);
49+
mongoc_client_destroy (client);
50+
51+
mongoc_cleanup ();
52+
53+
return 0;
54+
}

0 commit comments

Comments
 (0)