~ubuntu-branches/ubuntu/saucy/syslog-ng/saucy

« back to all changes in this revision

Viewing changes to modules/afmongodb/libmongo-client/docs/tutorial/tut_mongo_sync_query.h

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2011-11-15 08:48:02 UTC
  • mfrom: (26.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20111115084802-n0jegdnjlxk0m26s
Tags: 3.3.1.dfsg-1ubuntu1
* debian/control: remove libsystemd-daemon-dev build-depends
* debian/rules: remove --with-systemdsystemunitdir from
  override_dh_auto_configure.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @page tut_mongo_sync_query Querying documents
 
2
 *
 
3
 * We can connect, insert, and we still vaguely remember how to build
 
4
 * BSON objects: how about we go out and query the database this time?
 
5
 *
 
6
 * @dontinclude tut_mongo_sync.c
 
7
 * @skip tut_sync_insert
 
8
 * @skip }
 
9
 * @skip void
 
10
 * @until {
 
11
 *
 
12
 * First, we define a couple of variables: a connection, a packet, a
 
13
 * cursor, a BSON object and a counter:
 
14
 *
 
15
 * @until gint i
 
16
 *
 
17
 * We'll use the packet as an intermediate step between querying and
 
18
 * making a cursor. We'll see why later. The query will be used to
 
19
 * limit the documents queried, and the counter is just for pretty
 
20
 * printing.
 
21
 *
 
22
 * Then we do our routine connect:
 
23
 * @until }
 
24
 *
 
25
 * And then build a BSON object, an empty one, because an empty query
 
26
 * means we're interested in all of the documents!
 
27
 * @until bson_finish
 
28
 *
 
29
 * Now we have a connection, and we have a query, lets tell MongoDB
 
30
 * what we want:
 
31
 *
 
32
 * @until bson_free
 
33
 *
 
34
 * The first two parameters are obvious. The third is a set of flags -
 
35
 * but we don't use any right now. Then comes the number of documents
 
36
 * to skip, and the number of documents to return, followed by a
 
37
 * query, and an optional field selector, which we just left empty
 
38
 * (meaning we want all fields returned).
 
39
 *
 
40
 * There's more than one way to figure out the data returned by a
 
41
 * query: we can either use the returned packet as-is, and extract
 
42
 * data from it using the low-level mongo_wire family of functions. Or
 
43
 * we can make a cursor out of this packet, and iterate over the
 
44
 * elements:
 
45
 *
 
46
 * @until }
 
47
 *
 
48
 * @until }
 
49
 *
 
50
 * The first thing we do inside of the loop is to get the data from
 
51
 * the cursor - or bail out with an error if we can't.
 
52
 *
 
53
 * @until printf
 
54
 *
 
55
 * Then we proceed to make a BSON cursor, and print all the keys that
 
56
 * belong to the document.
 
57
 *
 
58
 * Once that's done, we free the resources we used, and continue along
 
59
 * the loop, until our cursor signals the end of the query.
 
60
 *
 
61
 * @until printf
 
62
 *
 
63
 * @until }
 
64
 *
 
65
 * Then we clean up and go home:
 
66
 * @until }
 
67
 */