File: mongoc_basic_aggregate.page

package info (click to toggle)
libmongoc 1.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,824 kB
  • ctags: 4,501
  • sloc: ansic: 57,956; makefile: 717; python: 502; sh: 54
file content (68 lines) | stat: -rw-r--r-- 3,684 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<page id="basic-aggregate-examples"
      type="topic"
      xmlns="http://projectmallard.org/1.0/">

  <title>Basic Aggregation Examples</title>
  <p>This document provides some practical, simple, examples to demonstrate the <code>distinct</code> and <code>mapReduce</code> commands.</p>

  <section id="setup">
    <title>Setup</title>
    <p>First we'll write some code to insert sample data:</p>
    <code mime="text/x-csrc"><include parse="text" href="../examples/doc-common-insert.c" xmlns="http://www.w3.org/2001/XInclude" /></code>
  </section>

  <section id="distinct">
    <title>"distinct" command</title>
    <p>This is how to use the <code>distinct</code> command to get the distinct values of <code>x</code> which are greater than <code>1</code>:</p>
    <code mime="text/x-csrc"><include parse="text" href="../examples/basic_aggregation/distinct.c" xmlns="http://www.w3.org/2001/XInclude" /></code>
  </section>

  <section id="map-reduce-basic">
    <title>"mapReduce" - basic example</title>
    <p>A simple example using the map reduce framework. It simply adds up the number of occurrences of each "tag".</p>

    <p>First define the <code>map</code> and <code>reduce</code> functions:</p>
    <code mime="text/x-csrc"><include parse="text" href="../examples/basic_aggregation/constants.c" xmlns="http://www.w3.org/2001/XInclude" /></code>

    <p>Run the <code>mapReduce</code> command:</p>
    <code mime="text/x-csrc"><include parse="text" href="../examples/basic_aggregation/map-reduce-basic.c" xmlns="http://www.w3.org/2001/XInclude" /></code>
  </section>

  <section id="map-reduce-advanced">
    <title>"mapReduce" - more complicated example</title>
    <p>You must have replica set running for this.</p>
    <p>In this example we contact a secondary in the replica set and do an "inline" map reduce, so the results are returned immediately:</p>
    <code mime="text/x-csrc"><include parse="text" href="../examples/basic_aggregation/map-reduce-advanced.c" xmlns="http://www.w3.org/2001/XInclude" /></code>
  </section>

  <section id="running">
    <title>Running</title>
    <p>Here's how to run the example code</p>
  <listing>
    <title>basic-aggregation.c</title>
    <code mime="text/x-csrc"><include parse="text" href="../examples/basic_aggregation/basic-aggregation.c" xmlns="http://www.w3.org/2001/XInclude" /></code>
  </listing>

  <p>If you want to try the advanced map reduce example with a secondary, start a replica set (instructions for how to do this can be found <link href="https://docs.mongodb.com/manual/tutorial/deploy-replica-set-for-testing/">here</link>).</p>

  <p>Otherwise, just start an instance of MongoDB:</p>
  <screen><output style="prompt">$ </output><input>mongod</input></screen>

  <p>Now compile and run the example program:</p>
  <screen><output style="prompt">$ </output><input>cd examples/basic_aggregation/</input>
<output style="prompt">$ </output><input>gcc -Wall -o agg-example basic-aggregation.c $(pkg-config --cflags --libs libmongoc-1.0)</input>
<output style="prompt">$ </output><input>./agg-example localhost</input>
Inserting data
distinct
Next double: 2.000000
Next double: 3.000000
map reduce
{ "result" : "outCollection", "timeMillis" : 155, "counts" : { "input" : 84, "emit" : 126, "reduce" : 3, "output" : 3 }, "ok" : 1 }
{ "_id" : "cat", "value" : 63 }
{ "_id" : "dog", "value" : 42 }
{ "_id" : "mouse", "value" : 21 }
more complicated map reduce
{ "results" : [ { "_id" : "cat", "value" : 63 }, { "_id" : "dog", "value" : 42 }, { "_id" : "mouse", "value" : 21 } ], "timeMillis" : 14, "counts" : { "input" : 84, "emit" : 126, "reduce" : 3, "output" : 3 }, "ok" : 1 }
</screen>
  </section>
</page>