1MONGOC_AGGREGATE(3) libmongoc MONGOC_AGGREGATE(3)
2
3
4
6 mongoc_aggregate - Aggregation Framework Examples
7
8 This document provides a number of practical examples that display the
9 capabilities of the aggregation framework.
10
11 The Aggregations using the Zip Codes Data Set examples uses a publicly
12 available data set of all zipcodes and populations in the United
13 States. These data are available at: zips.json.
14
16 Let's check if everything is installed.
17
18 Use the following command to load zips.json data set into mongod in‐
19 stance:
20
21 $ mongoimport --drop -d test -c zipcodes zips.json
22
23 Let's use the MongoDB shell to verify that everything was imported suc‐
24 cessfully.
25
26 $ mongo test
27 connecting to: test
28 > db.zipcodes.count()
29 29467
30 > db.zipcodes.findOne()
31 {
32 "_id" : "35004",
33 "city" : "ACMAR",
34 "loc" : [
35 -86.51557,
36 33.584132
37 ],
38 "pop" : 6055,
39 "state" : "AL"
40 }
41
43 Each document in this collection has the following form:
44
45 {
46 "_id" : "35004",
47 "city" : "Acmar",
48 "state" : "AL",
49 "pop" : 6055,
50 "loc" : [-86.51557, 33.584132]
51 }
52
53 In these documents:
54
55 • The _id field holds the zipcode as a string.
56
57 • The city field holds the city name.
58
59 • The state field holds the two letter state abbreviation.
60
61 • The pop field holds the population.
62
63 • The loc field holds the location as a [latitude, longitude] array.
64
66 To get all states with a population greater than 10 million, use the
67 following aggregation pipeline:
68
69 aggregation1.c
70
71 #include <mongoc/mongoc.h>
72 #include <stdio.h>
73
74 static void
75 print_pipeline (mongoc_collection_t *collection)
76 {
77 mongoc_cursor_t *cursor;
78 bson_error_t error;
79 const bson_t *doc;
80 bson_t *pipeline;
81 char *str;
82
83 pipeline = BCON_NEW ("pipeline",
84 "[",
85 "{",
86 "$group",
87 "{",
88 "_id",
89 "$state",
90 "total_pop",
91 "{",
92 "$sum",
93 "$pop",
94 "}",
95 "}",
96 "}",
97 "{",
98 "$match",
99 "{",
100 "total_pop",
101 "{",
102 "$gte",
103 BCON_INT32 (10000000),
104 "}",
105 "}",
106 "}",
107 "]");
108
109 cursor = mongoc_collection_aggregate (
110 collection, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
111
112 while (mongoc_cursor_next (cursor, &doc)) {
113 str = bson_as_canonical_extended_json (doc, NULL);
114 printf ("%s\n", str);
115 bson_free (str);
116 }
117
118 if (mongoc_cursor_error (cursor, &error)) {
119 fprintf (stderr, "Cursor Failure: %s\n", error.message);
120 }
121
122 mongoc_cursor_destroy (cursor);
123 bson_destroy (pipeline);
124 }
125
126 int
127 main (int argc, char *argv[])
128 {
129 mongoc_client_t *client;
130 mongoc_collection_t *collection;
131 const char *uri_string =
132 "mongodb://localhost:27017/?appname=aggregation-example";
133 mongoc_uri_t *uri;
134 bson_error_t error;
135
136 mongoc_init ();
137
138 uri = mongoc_uri_new_with_error (uri_string, &error);
139 if (!uri) {
140 fprintf (stderr,
141 "failed to parse URI: %s\n"
142 "error message: %s\n",
143 uri_string,
144 error.message);
145 return EXIT_FAILURE;
146 }
147
148 client = mongoc_client_new_from_uri (uri);
149 if (!client) {
150 return EXIT_FAILURE;
151 }
152
153 mongoc_client_set_error_api (client, 2);
154 collection = mongoc_client_get_collection (client, "test", "zipcodes");
155
156 print_pipeline (collection);
157
158 mongoc_uri_destroy (uri);
159 mongoc_collection_destroy (collection);
160 mongoc_client_destroy (client);
161
162 mongoc_cleanup ();
163
164 return EXIT_SUCCESS;
165 }
166
167
168 You should see a result like the following:
169
170 { "_id" : "PA", "total_pop" : 11881643 }
171 { "_id" : "OH", "total_pop" : 10847115 }
172 { "_id" : "NY", "total_pop" : 17990455 }
173 { "_id" : "FL", "total_pop" : 12937284 }
174 { "_id" : "TX", "total_pop" : 16986510 }
175 { "_id" : "IL", "total_pop" : 11430472 }
176 { "_id" : "CA", "total_pop" : 29760021 }
177
178 The above aggregation pipeline is build from two pipeline operators:
179 $group and $match.
180
181 The $group pipeline operator requires _id field where we specify group‐
182 ing; remaining fields specify how to generate composite value and must
183 use one of the group aggregation functions: $addToSet, $first, $last,
184 $max, $min, $avg, $push, $sum. The $match pipeline operator syntax is
185 the same as the read operation query syntax.
186
187 The $group process reads all documents and for each state it creates a
188 separate document, for example:
189
190 { "_id" : "WA", "total_pop" : 4866692 }
191
192 The total_pop field uses the $sum aggregation function to sum the val‐
193 ues of all pop fields in the source documents.
194
195 Documents created by $group are piped to the $match pipeline operator.
196 It returns the documents with the value of total_pop field greater than
197 or equal to 10 million.
198
200 To get the first three states with the greatest average population per
201 city, use the following aggregation:
202
203 pipeline = BCON_NEW ("pipeline", "[",
204 "{", "$group", "{", "_id", "{", "state", "$state", "city", "$city", "}", "pop", "{", "$sum", "$pop", "}", "}", "}",
205 "{", "$group", "{", "_id", "$_id.state", "avg_city_pop", "{", "$avg", "$pop", "}", "}", "}",
206 "{", "$sort", "{", "avg_city_pop", BCON_INT32 (-1), "}", "}",
207 "{", "$limit", BCON_INT32 (3) "}",
208 "]");
209
210 This aggregate pipeline produces:
211
212 { "_id" : "DC", "avg_city_pop" : 303450.0 }
213 { "_id" : "FL", "avg_city_pop" : 27942.29805615551 }
214 { "_id" : "CA", "avg_city_pop" : 27735.341099720412 }
215
216 The above aggregation pipeline is build from three pipeline operators:
217 $group, $sort and $limit.
218
219 The first $group operator creates the following documents:
220
221 { "_id" : { "state" : "WY", "city" : "Smoot" }, "pop" : 414 }
222
223 Note, that the $group operator can't use nested documents except the
224 _id field.
225
226 The second $group uses these documents to create the following docu‐
227 ments:
228
229 { "_id" : "FL", "avg_city_pop" : 27942.29805615551 }
230
231 These documents are sorted by the avg_city_pop field in descending or‐
232 der. Finally, the $limit pipeline operator returns the first 3 docu‐
233 ments from the sorted set.
234
236 MongoDB, Inc
237
239 2017-present, MongoDB, Inc
240
241
242
243
2441.17.6 Jun 03, 2021 MONGOC_AGGREGATE(3)