1MONGOC_AGGREGATE(3)            MongoDB C Driver            MONGOC_AGGREGATE(3)
2
3
4

NAME

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

REQUIREMENTS

16       Let's check if everything is installed.
17
18       Use the following command  to  load  zips.json  data  set  into  mongod
19       instance:
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

AGGREGATIONS USING THE ZIP CODES DATA SET

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

STATES WITH POPULATIONS OVER 10 MILLION

66       To get all states with a population greater than 10  million,  use  the
67       following aggregation pipeline: aggregation1.c.INDENT 0.0
68
69          #include <mongoc/mongoc.h>
70          #include <stdio.h>
71
72          static void
73          print_pipeline (mongoc_collection_t *collection)
74          {
75             mongoc_cursor_t *cursor;
76             bson_error_t error;
77             const bson_t *doc;
78             bson_t *pipeline;
79             char *str;
80
81             pipeline = BCON_NEW ("pipeline",
82                                  "[",
83                                  "{",
84                                  "$group",
85                                  "{",
86                                  "_id",
87                                  "$state",
88                                  "total_pop",
89                                  "{",
90                                  "$sum",
91                                  "$pop",
92                                  "}",
93                                  "}",
94                                  "}",
95                                  "{",
96                                  "$match",
97                                  "{",
98                                  "total_pop",
99                                  "{",
100                                  "$gte",
101                                  BCON_INT32 (10000000),
102                                  "}",
103                                  "}",
104                                  "}",
105                                  "]");
106
107             cursor = mongoc_collection_aggregate (
108                collection, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
109
110             while (mongoc_cursor_next (cursor, &doc)) {
111                str = bson_as_canonical_extended_json (doc, NULL);
112                printf ("%s\n", str);
113                bson_free (str);
114             }
115
116             if (mongoc_cursor_error (cursor, &error)) {
117                fprintf (stderr, "Cursor Failure: %s\n", error.message);
118             }
119
120             mongoc_cursor_destroy (cursor);
121             bson_destroy (pipeline);
122          }
123
124          int
125          main (int argc, char *argv[])
126          {
127             mongoc_client_t *client;
128             mongoc_collection_t *collection;
129             const char *uri_string =
130                "mongodb://localhost:27017/?appname=aggregation-example";
131             mongoc_uri_t *uri;
132             bson_error_t error;
133
134             mongoc_init ();
135
136             uri = mongoc_uri_new_with_error (uri_string, &error);
137             if (!uri) {
138                fprintf (stderr,
139                         "failed to parse URI: %s\n"
140                         "error message:       %s\n",
141                         uri_string,
142                         error.message);
143                return EXIT_FAILURE;
144             }
145
146             client = mongoc_client_new_from_uri (uri);
147             if (!client) {
148                return EXIT_FAILURE;
149             }
150
151             mongoc_client_set_error_api (client, 2);
152             collection = mongoc_client_get_collection (client, "test", "zipcodes");
153
154             print_pipeline (collection);
155
156             mongoc_uri_destroy (uri);
157             mongoc_collection_destroy (collection);
158             mongoc_client_destroy (client);
159
160             mongoc_cleanup ();
161
162             return EXIT_SUCCESS;
163          }
164
165
166You should see a result like the following:
167
168          { "_id" : "PA", "total_pop" : 11881643 }
169          { "_id" : "OH", "total_pop" : 10847115 }
170          { "_id" : "NY", "total_pop" : 17990455 }
171          { "_id" : "FL", "total_pop" : 12937284 }
172          { "_id" : "TX", "total_pop" : 16986510 }
173          { "_id" : "IL", "total_pop" : 11430472 }
174          { "_id" : "CA", "total_pop" : 29760021 }
175
176       The  above  aggregation  pipeline is build from two pipeline operators:
177       $group and $match.
178
179       The $group pipeline operator requires _id field where we specify group‐
180       ing;  remaining fields specify how to generate composite value and must
181       use one of the group aggregation functions: $addToSet,  $first,  $last,
182       $max,  $min,  $avg, $push, $sum. The $match pipeline operator syntax is
183       the same as the read operation query syntax.
184
185       The $group process reads all documents and for each state it creates  a
186       separate document, for example:
187
188          { "_id" : "WA", "total_pop" : 4866692 }
189
190       The  total_pop field uses the $sum aggregation function to sum the val‐
191       ues of all pop fields in the source documents.
192
193       Documents created by $group are piped to the $match pipeline  operator.
194       It returns the documents with the value of total_pop field greater than
195       or equal to 10 million.
196

AVERAGE CITY POPULATION BY STATE

198       To get the first three states with the greatest average population  per
199       city, use the following aggregation:
200
201          pipeline = BCON_NEW ("pipeline", "[",
202             "{", "$group", "{", "_id", "{", "state", "$state", "city", "$city", "}", "pop", "{", "$sum", "$pop", "}", "}", "}",
203             "{", "$group", "{", "_id", "$_id.state", "avg_city_pop", "{", "$avg", "$pop", "}", "}", "}",
204             "{", "$sort", "{", "avg_city_pop", BCON_INT32 (-1), "}", "}",
205             "{", "$limit", BCON_INT32 (3) "}",
206          "]");
207
208       This aggregate pipeline produces:
209
210          { "_id" : "DC", "avg_city_pop" : 303450.0 }
211          { "_id" : "FL", "avg_city_pop" : 27942.29805615551 }
212          { "_id" : "CA", "avg_city_pop" : 27735.341099720412 }
213
214       The  above aggregation pipeline is build from three pipeline operators:
215       $group, $sort and $limit.
216
217       The first $group operator creates the following documents:
218
219          { "_id" : { "state" : "WY", "city" : "Smoot" }, "pop" : 414 }
220
221       Note, that the $group operator can't use nested  documents  except  the
222       _id field.
223
224       The  second  $group  uses these documents to create the following docu‐
225       ments:
226
227          { "_id" : "FL", "avg_city_pop" : 27942.29805615551 }
228
229       These documents are sorted by  the  avg_city_pop  field  in  descending
230       order.  Finally, the $limit pipeline operator returns the first 3 docu‐
231       ments from the sorted set.
232

AUTHOR

234       MongoDB, Inc
235
237       2017-present, MongoDB, Inc
238
239
240
241
2421.14.0                           Feb 22, 2019              MONGOC_AGGREGATE(3)
Impressum