File: mongoc_common_task_examples.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 (126 lines) | stat: -rw-r--r-- 4,451 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<page id="mongoc-common-task-examples"
      type="topic"
      xmlns="http://projectmallard.org/1.0/">
  <info>
    <link type="guide" xref="index#basic-operations"/>
  </info>
  <title>Common Tasks</title>

  <section id="common-tasks">
  <p>Drivers for some other languages provide helper functions to perform certain common tasks. In the C Driver we must explicitly build commands to send to the server.</p>
  <p>This snippet contains example code for the <code>explain</code>, <code>copydb</code> and <code>cloneCollection</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="explain">
    <title>"explain" Command</title>
  <p>This is how to use the <code>explain</code> command in MongoDB 3.2+:</p>
  <code mime="text/x-csrc"><include parse="text" href="../examples/common_operations/explain.c" xmlns="http://www.w3.org/2001/XInclude" /></code>
  </section>

  <section id="copydb">
    <title>"copydb" Command</title>
      <p>This example requires two instances of mongo to be running.</p>
      <p>Here's how to use the <code>copydb</code> command to copy a database from another instance of MongoDB:</p>
    <code mime="text/x-csrc"><include parse="text" href="../examples/common_operations/copydb.c" xmlns="http://www.w3.org/2001/XInclude" /></code>
  </section>

  <section id="clone-collection">
    <title>"cloneCollection" Command</title>
    <p>This example requires two instances of mongo to be running.</p>
    <p>Here's an example of the <code>cloneCollection</code> command to clone a collection from another instance of MongoDB:</p>


  <code mime="text/x-csrc"><include parse="text" href="../examples/common_operations/clone-collection.c" xmlns="http://www.w3.org/2001/XInclude" /></code>
  </section>

  <section id="main">
    <title>Running</title>
  <listing>
    <title>examples/common-operations.c</title>
    <code mime="text/x-csrc"><include parse="text" href="../examples/common_operations/common-operations.c" xmlns="http://www.w3.org/2001/XInclude" /></code>
  </listing>

  <p>First launch two separate instances of mongod (must be done from separate shells):</p>
  <screen><output style="prompt">$ </output><input>mongod</input></screen>
  <screen><output style="prompt">$ </output><input>mkdir /tmp/db2</input>
<output style="prompt">$ </output><input>mongod --dbpath /tmp/db2 --port 27018 # second instance</input></screen>

  <p>Now compile and run the example program:</p>
  <screen><output style="prompt">$ </output><input>cd examples/common_operations/</input>
<output style="prompt">$ </output><input>gcc -Wall -o example common-operations.c $(pkg-config --cflags --libs libmongoc-1.0)</input>
<output style="prompt">$ </output><input>./example localhost:27017 localhost:27018</input>
<output>
Inserting data
explain
{
   "executionStats" : {
      "allPlansExecution" : [],
      "executionStages" : {
         "advanced" : 19,
         "direction" : "forward" ,
         "docsExamined" : 76,
         "executionTimeMillisEstimate" : 0,
         "filter" : {
            "x" : {
               "$eq" : 1
            }
         },
         "invalidates" : 0,
         "isEOF" : 1,
         "nReturned" : 19,
         "needTime" : 58,
         "needYield" : 0,
         "restoreState" : 0,
         "saveState" : 0,
         "stage" : "COLLSCAN" ,
         "works" : 78
      },
      "executionSuccess" : true,
      "executionTimeMillis" : 0,
      "nReturned" : 19,
      "totalDocsExamined" : 76,
      "totalKeysExamined" : 0
   },
   "ok" : 1,
   "queryPlanner" : {
      "indexFilterSet" : false,
      "namespace" : "test.things",
      "parsedQuery" : {
         "x" : {
            "$eq" : 1
         }
      },
      "plannerVersion" : 1,
      "rejectedPlans" : [],
      "winningPlan" : {
         "direction" : "forward" ,
         "filter" : {
            "x" : {
               "$eq" : 1
            }
         },
         "stage" : "COLLSCAN"
      }
   },
   "serverInfo" : {
      "gitVersion" : "05552b562c7a0b3143a729aaa0838e558dc49b25" ,
      "host" : "MacBook-Pro-57.local",
      "port" : 27017,
      "version" : "3.2.6"
   }
}
copydb
{ "ok" : 1 }
clone collection
{ "ok" : 1 }
</output>

  </screen>
  </section>
  </section>
</page>