Skip to content

Commit 7eac27d

Browse files
committed
CDRIVER-1979 convert docs from XML to RST
1 parent c63a60e commit 7eac27d

File tree

935 files changed

+19371
-21842
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

935 files changed

+19371
-21842
lines changed

doc/advanced-connections.rst

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
:man_page: mongoc_advanced_connections
2+
3+
Advanced Connections
4+
====================
5+
6+
The following guide contains information specific to certain types of MongoDB configurations.
7+
8+
For an example of connecting to a simple standalone server, see the :ref:`Tutorial <tutorial_connecting>`. To establish a connection with authentication options enabled, see the :doc:`Authentication <authentication>` page.
9+
10+
Connecting to a Replica Set
11+
---------------------------
12+
13+
Connecting to a `replica set <http://docs.mongodb.org/manual/replication/>`_ is much like connecting to a standalone MongoDB server. Simply specify the replica set name using the ``?replicaSet=myreplset`` URI option.
14+
15+
.. code-block:: c
16+
17+
#include <bson.h>
18+
#include <mongoc.h>
19+
20+
int
21+
main (int argc, char *argv[])
22+
{
23+
mongoc_client_t *client;
24+
25+
mongoc_init ();
26+
27+
/* Create our MongoDB Client */
28+
client = mongoc_client_new (
29+
"mongodb://host01:27017,host02:27017,host03:27017/?replicaSet=myreplset");
30+
31+
/* Do some work */
32+
/* TODO */
33+
34+
/* Clean up */
35+
mongoc_client_destroy (client);
36+
mongoc_cleanup ();
37+
38+
return 0;
39+
}
40+
41+
.. tip::
42+
43+
Multiple hostnames can be specified in the MongoDB connection string URI, with a comma separating hosts in the seed list.
44+
45+
It is recommended to use a seed list of members of the replica set to allow the driver to connect to any node.
46+
47+
Connecting to a Sharded Cluster
48+
-------------------------------
49+
50+
To connect to a `sharded cluster <http://docs.mongodb.org/manual/sharding/>`_, specify the ``mongos`` nodes the client should connect to. The C Driver will automatically detect that it has connected to a ``mongos`` sharding server.
51+
52+
If more than one hostname is specified, a seed list will be created to attempt failover between the ``mongos`` instances.
53+
54+
.. warning::
55+
56+
Specifying the ``replicaSet`` parameter when connecting to a ``mongos`` sharding server is invalid.
57+
58+
.. code-block:: c
59+
60+
#include <bson.h>
61+
#include <mongoc.h>
62+
63+
int
64+
main (int argc, char *argv[])
65+
{
66+
mongoc_client_t *client;
67+
68+
mongoc_init ();
69+
70+
/* Create our MongoDB Client */
71+
client = mongoc_client_new ("mongodb://myshard01:27017/");
72+
73+
/* Do something with client ... */
74+
75+
/* Free the client */
76+
mongoc_client_destroy (client);
77+
78+
mongoc_cleanup ();
79+
80+
return 0;
81+
}
82+
83+
Connecting to an IPv6 Address
84+
-----------------------------
85+
86+
The MongoDB C Driver will automatically resolve IPv6 addresses from host names. However, to specify an IPv6 address directly, wrap the address in ``[]``.
87+
88+
.. code-block:: none
89+
90+
mongoc_uri_t *uri = mongoc_uri_new ("mongodb://[::1]:27017");
91+
92+
Connecting to a UNIX Domain Socket
93+
----------------------------------
94+
95+
On UNIX-like systems, the C Driver can connect directly to a MongoDB server using a UNIX domain socket. Pass the URL-encoded path to the socket, which *must* be suffixed with ``.sock``. For example, to connect to a domain socket at ``/tmp/mongodb-27017.sock``:
96+
97+
.. code-block:: none
98+
99+
mongoc_uri_t *uri = mongoc_uri_new ("mongodb://%2Ftmp%2Fmongodb-27017.sock");
100+
101+
Include username and password like so:
102+
103+
.. code-block:: none
104+
105+
mongoc_uri_t *uri = mongoc_uri_new ("mongodb://user:pass@%2Ftmp%2Fmongodb-27017.sock");
106+
107+
Connecting to a server over SSL
108+
-------------------------------
109+
110+
These are instructions for configuring TLS/SSL connections.
111+
112+
To run a server locally (on port 27017, for example):
113+
114+
.. code-block:: none
115+
116+
$ mongod --port 27017 --sslMode requireSSL --sslPEMKeyFile server.pem --sslCAFile ca.pem
117+
118+
Add ``/?ssl=true`` to the end of a client URI.
119+
120+
.. code-block:: none
121+
122+
mongoc_client_t *client = NULL;
123+
client = mongoc_client_new ("mongodb://localhost:27017/?ssl=true");
124+
125+
MongoDB requires client certificates by default, unless the ``--sslAllowConnectionsWithoutCertificates`` is provided. The C Driver can be configured to present a client certificate using a ``mongoc_ssl_opt_t``:
126+
127+
.. code-block:: none
128+
129+
const mongoc_ssl_opt_t *ssl_default = mongoc_ssl_opt_get_default ();
130+
mongoc_ssl_opt_t ssl_opts = { 0 };
131+
132+
/* optionally copy in a custom trust directory or file; otherwise the default is used. */
133+
memcpy (&ssl_opts, ssl_default, sizeof ssl_opts);
134+
ssl_opts.pem_file = "client.pem"
135+
136+
mongoc_client_set_ssl_opts (client, &ssl_opts);
137+
138+
The client certificate provided by ``pem_file`` must be issued by one of the server trusted Certificate Authorities listed in ``--sslCAFile``, or issued by a CA in the native certificate store on the server when omitted.
139+
140+
To verify the server certificate against a specific CA, provide a PEM armored file with a CA certificate, or contatinated list of CA certificates using the ``ca_file`` option, or ``c_rehash`` directory structure of CAs, pointed to using the ``ca_dir`` option. When no ``ca_file`` or ``ca_dir`` is provided, the driver will use CAs provided by the native platform certificate store.
141+
142+
See :doc:`mongoc_ssl_opt_t` for more information on the various SSL related options.
143+
144+
Additional Connection Options
145+
-----------------------------
146+
147+
A variety of connection options for the MongoDB URI can be found `here <http://docs.mongodb.org/manual/reference/connection-string/>`_.
148+

doc/aggregate.rst

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
:man_page: mongoc_aggregate
2+
3+
Aggregation Framework Examples
4+
==============================
5+
6+
This document provides a number of practical examples that display the capabilities of the aggregation framework.
7+
8+
The `Aggregations using the Zip Codes Data Set <https://docs.mongodb.org/manual/tutorial/aggregation-zip-code-data-set/>`_ examples uses a publicly available data set of all zipcodes and populations in the United States. These data are available at: `zips.json <http://media.mongodb.org/zips.json>`_.
9+
10+
Requirements
11+
------------
12+
13+
`MongoDB <https://www.mongodb.org>`_, version 2.2.0 or later. `MongoDB C driver <https://github.com/mongodb/mongo-c-driver>`_, version 0.96.0 or later.
14+
15+
Let's check if everything is installed.
16+
17+
Use the following command to load zips.json data set into mongod instance:
18+
19+
.. code-block:: none
20+
21+
$ mongoimport --drop -d test -c zipcodes zips.json
22+
23+
Let's use the MongoDB shell to verify that everything was imported successfully.
24+
25+
.. code-block:: none
26+
27+
$ mongo testMongoDB shell version: 2.6.1
28+
connecting to: test> db.zipcodes.count()29467> db.zipcodes.findOne(){
29+
"_id" : "35004",
30+
"city" : "ACMAR",
31+
"loc" : [
32+
-86.51557,
33+
33.584132
34+
],
35+
"pop" : 6055,
36+
"state" : "AL"
37+
}
38+
39+
Aggregations using the Zip Codes Data Set
40+
-----------------------------------------
41+
42+
Each document in this collection has the following form:
43+
44+
.. code-block:: none
45+
46+
{
47+
"_id" : "35004",
48+
"city" : "Acmar",
49+
"state" : "AL",
50+
"pop" : 6055,
51+
"loc" : [-86.51557, 33.584132]
52+
}
53+
54+
In these documents:
55+
56+
* The ``_id`` field holds the zipcode as a string.
57+
* The ``city`` field holds the city name.
58+
* The ``state`` field holds the two letter state abbreviation.
59+
* The ``pop`` field holds the population.
60+
* The ``loc`` field holds the location as a ``[latitude, longitude]`` array.
61+
62+
States with Populations Over 10 Million
63+
---------------------------------------
64+
65+
To get all states with a population greater than 10 million, use the following aggregation pipeline:
66+
67+
.. literalinclude:: ../examples/aggregation/aggregation1.c
68+
:language: c
69+
:caption: aggregation1.c
70+
71+
You should see a result like the following:
72+
73+
.. code-block:: none
74+
75+
{ "_id" : "PA", "total_pop" : 11881643 }
76+
{ "_id" : "OH", "total_pop" : 10847115 }
77+
{ "_id" : "NY", "total_pop" : 17990455 }
78+
{ "_id" : "FL", "total_pop" : 12937284 }
79+
{ "_id" : "TX", "total_pop" : 16986510 }
80+
{ "_id" : "IL", "total_pop" : 11430472 }
81+
{ "_id" : "CA", "total_pop" : 29760021 }
82+
83+
The above aggregation pipeline is build from two pipeline operators: ``$group`` and ``$match``.
84+
85+
The ``$group`` pipeline operator requires _id field where we specify grouping; remaining fields specify how to generate composite value and must use one of the group aggregation functions: ``$addToSet``, ``$first``, ``$last``, ``$max``, ``$min``, ``$avg``, ``$push``, ``$sum``. The ``$match`` pipeline operator syntax is the same as the read operation query syntax.
86+
87+
The ``$group`` process reads all documents and for each state it creates a separate document, for example:
88+
89+
.. code-block:: none
90+
91+
{ "_id" : "WA", "total_pop" : 4866692 }
92+
93+
The ``total_pop`` field uses the $sum aggregation function to sum the values of all pop fields in the source documents.
94+
95+
Documents created by ``$group`` are piped to the ``$match`` pipeline operator. It returns the documents with the value of ``total_pop`` field greater than or equal to 10 million.
96+
97+
Average City Population by State
98+
--------------------------------
99+
100+
To get the first three states with the greatest average population per city, use the following aggregation:
101+
102+
.. code-block:: none
103+
104+
pipeline = BCON_NEW ("pipeline", "[",
105+
"{", "$group", "{", "_id", "{", "state", "$state", "city", "$city", "}", "pop", "{", "$sum", "$pop", "}", "}", "}",
106+
"{", "$group", "{", "_id", "$_id.state", "avg_city_pop", "{", "$avg", "$pop", "}", "}", "}",
107+
"{", "$sort", "{", "avg_city_pop", BCON_INT32 (-1), "}", "}",
108+
"{", "$limit", BCON_INT32 (3) "}",
109+
"]");
110+
111+
This aggregate pipeline produces:
112+
113+
.. code-block:: none
114+
115+
{ "_id" : "DC", "avg_city_pop" : 303450.0 }
116+
{ "_id" : "FL", "avg_city_pop" : 27942.29805615551 }
117+
{ "_id" : "CA", "avg_city_pop" : 27735.341099720412 }
118+
119+
The above aggregation pipeline is build from three pipeline operators: ``$group``, ``$sort`` and ``$limit``.
120+
121+
The first ``$group`` operator creates the following documents:
122+
123+
.. code-block:: none
124+
125+
{ "_id" : { "state" : "WY", "city" : "Smoot" }, "pop" : 414 }
126+
127+
Note, that the ``$group`` operator can't use nested documents except the ``_id`` field.
128+
129+
The second ``$group`` uses these documents to create the following documents:
130+
131+
.. code-block:: none
132+
133+
{ "_id" : "FL", "avg_city_pop" : 27942.29805615551 }
134+
135+
These documents are sorted by the ``avg_city_pop`` field in descending order. Finally, the ``$limit`` pipeline operator returns the first 3 documents from the sorted set.
136+

0 commit comments

Comments
 (0)