summaryrefslogtreecommitdiff
path: root/docs/tutorial/tut_mongo_sync_cmd_custom.h
blob: 0b224b21c344468831113f335c685676f293f4e7 (plain)
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
/** @page tut_mongo_sync_cmd_custom Running custom commands
 *
 * Sometimes it is necessary to run custom commands against a
 * database, commands for which the library does not provide a
 * convenience wrapper for. In this tutorial, we will explore how to
 * run server-side evaluations, using the @a $eval command.
 *
 * @dontinclude tut_mongo_sync_cmd_custom.c
 * @until stdlib.h
 *
 * @until eval
 *
 * First, we connect to the database, and create a BSON object that
 * will hold our command, one that creates a function server side,
 * that takes one argument, and returns the argument plus 4.2. The
 * BSON object will also set up the arguments passed to this function,
 * which, in our case, will be the number @a 1.
 *
 * @line conn =
 * @until }
 *
 * @line eval =
 * @until bson_finish
 *
 * Once we have the connection and the query established, it is time
 * to send the command:
 *
 * @line p =
 * @until }
 *
 * We then create a cursor from the returned packet, and iterate over
 * it (in case there are multiple documents returned - which will not
 * be the case here):
 *
 * @line cursor =
 * @until }
 *
 * @until gdouble
 *
 * We want to retrieve each document, and find the @a retval key in
 * them, because that's where @a $eval returns the results to us.
 *
 * @line result =
 * @until }
 * @until }
 *
 * At this point, we have successfully extracted the data, so we can
 * free up the BSON and cursor objects.
 *
 * @line bson_cursor_free
 * @until bson_free
 *
 * And finally, print the result:
 *
 * @until printf
 *
 * @until }
 *
 * And that's it! We clean up, disconnect, and that's all there is to
 * running custom commands!
 *
 * @line mongo_sync_cursor_free
 * @until }
 */