summaryrefslogtreecommitdiff
path: root/docs/tutorial/examples/tut_mongo_sync_cmd_custom.c
blob: 4e48b187420bb359d785c72fd699878b878af28e (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <mongo.h>

#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int
main (void)
{
  mongo_sync_connection *conn;
  mongo_packet *p;
  mongo_sync_cursor *cursor;
  bson *eval;

  conn = mongo_sync_connect ("localhost", 27017, FALSE);
  if (!conn)
    {
      perror ("mongo_sync_connect()");
      exit (1);
    }

  eval = bson_build_full (BSON_TYPE_JS_CODE, "$eval", FALSE,
                          "function(x){return x + 4.2;}", -1,
                          BSON_TYPE_ARRAY, "args", TRUE,
                          bson_build (BSON_TYPE_INT32, "0", 1,
                                      BSON_TYPE_NONE),
                          BSON_TYPE_NONE);
  bson_finish (eval);

  p = mongo_sync_cmd_custom (conn, "test", eval);

  if (!p)
    {
      gchar *error = NULL;

      mongo_sync_cmd_get_last_error (conn, "test", &error);
      fprintf (stderr, "Can't run db.eval: %s\n", error);
      g_free (error);

      exit (1);
    }

  cursor = mongo_sync_cursor_new (conn, "test", p);

  if (!cursor)
    {
      perror ("mongo_sync_cursor_new()");
      exit (1);
    }

  while (mongo_sync_cursor_next (cursor))
    {
      bson *result;
      bson_cursor *c;
      gdouble r;

      result = mongo_sync_cursor_get_data (cursor);
      if (!result)
        {
          perror ("mongo_sync_cursor_get_data()");
          exit (1);
        }

      c = bson_find (result, "retval");
      if (!bson_cursor_get_double (c, &r))
        {
          perror ("bson_cursor_get_double()");
          exit (1);
        }
      bson_cursor_free (c);
      bson_free (result);

      printf ("Result: %2.1f\n", r);
    }

  mongo_sync_cursor_free (cursor);
  mongo_sync_disconnect (conn);

  return 0;
}