Beispiel #1
0
static void
test_code (void)
{
   const char *val;

   bson_t *bcon = BCON_NEW ("foo", BCON_CODE ("var a = {};"));

   assert (BCON_EXTRACT (bcon, "foo", BCONE_CODE (val)));

   assert (strcmp (val, "var a = {};") == 0);

   bson_destroy (bcon);
}
Beispiel #2
0
static void
test_code (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_code (&expected, "foo", -1, "var a = {};");

   BCON_APPEND (&bcon, "foo", BCON_CODE ("var a = {};"));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Beispiel #3
0
static void
test_value_basic (void)
{
   static const uint8_t raw[16] = { 0 };
   const bson_value_t *value;
   bson_value_t copy;
   bson_iter_t iter;
   bson_oid_t oid;
   bson_t other = BSON_INITIALIZER;
   bson_t *doc;
   bson_t sub = BSON_INITIALIZER;
   bool r;
   int i;

   bson_oid_init (&oid, NULL);

   doc = BCON_NEW ("double", BCON_DOUBLE (123.4),
                   "utf8", "this is my string",
                   "document", BCON_DOCUMENT (&sub),
                   "array", BCON_DOCUMENT (&sub),
                   "binary", BCON_BIN (BSON_SUBTYPE_BINARY, raw, sizeof raw),
                   "undefined", BCON_UNDEFINED,
                   "oid", BCON_OID (&oid),
                   "bool", BCON_BOOL (true),
                   "datetime", BCON_DATE_TIME (12345678),
                   "null", BCON_NULL,
                   "regex", BCON_REGEX ("^hello", "i"),
                   "dbpointer", BCON_DBPOINTER ("test.test", &oid),
                   "code", BCON_CODE ("var a = function() {}"),
                   "symbol", BCON_SYMBOL ("my_symbol"),
                   "codewscope", BCON_CODEWSCOPE ("var a = 1;", &sub),
                   "int32", BCON_INT32 (1234),
                   "timestamp", BCON_TIMESTAMP (1234, 4567),
                   "int64", BCON_INT32 (4321),
                   "maxkey", BCON_MAXKEY,
                   "minkey", BCON_MINKEY);

   r = bson_iter_init (&iter, doc);
   assert (r);

   for (i = 0; i < 20; i++) {
      r = bson_iter_next (&iter);
      assert (r);

      value = bson_iter_value (&iter);
      assert (value);

      bson_value_copy (value, &copy);

      r = bson_append_value (&other, bson_iter_key (&iter), -1, &copy);
      assert (r);

      bson_value_destroy (&copy);
   }

   r = bson_iter_next (&iter);
   assert (!r);

   bson_destroy (doc);
   bson_destroy (&other);
}
bool map_reduce_basic (mongoc_database_t* database)
{
   bson_t reply;
   bson_t* command;
   bool res;
   bson_error_t error;
   mongoc_cursor_t* cursor;
   const bson_t* doc;

   bool map_reduce_done = false;
   bool query_done = false;

   const char* out_collection_name = "outCollection";
   mongoc_collection_t* out_collection;

   /* Empty find query */
   bson_t find_query = BSON_INITIALIZER;

   /* Construct the mapReduce command */

   /* Other arguments can also be specified here, like "query" or
      "limit" and so on */
   command = BCON_NEW ("mapReduce", BCON_UTF8 (COLLECTION_NAME),
                       "map", BCON_CODE (MAPPER),
                       "reduce", BCON_CODE (REDUCER),
                       "out", BCON_UTF8 (out_collection_name));
   res = mongoc_database_command_simple (database, command, NULL,
                                        &reply, &error);
   map_reduce_done = true;

   if (!res) {
      fprintf (stderr, "MapReduce failed: %s\n", error.message);
      goto cleanup;
   }

   /* Do something with the reply (it doesn't contain the mapReduce results) */
   print_res (&reply);

   /* Now we'll query outCollection to see what the results are */
   out_collection = mongoc_database_get_collection (database,
                                                    out_collection_name);
   cursor = mongoc_collection_find_with_opts (out_collection, &find_query,
                                              NULL, NULL);
   query_done = true;

   /* Do something with the results */
   while (mongoc_cursor_next (cursor, &doc)) {
      print_res (doc);
   }

   if (mongoc_cursor_error (cursor, &error)) {
      fprintf (stderr, "ERROR: %s\n", error.message);
      res = false;
      goto cleanup;
   }

cleanup:
   /* cleanup */
   if (query_done) {
      mongoc_cursor_destroy (cursor);
      mongoc_collection_destroy (out_collection);
   }

   if (map_reduce_done) {
      bson_destroy (&reply);
      bson_destroy (command);
   }

   return res;
}