void PruebaMongoDB() { mongoc_collection_t *collection; mongoc_client_t *client; mongoc_cursor_t *cursor; const bson_t *item; bson_error_t error; bson_oid_t oid; bson_t *query; bson_t *doc; char *str; bool r; mongoc_init(); /* get a handle to our collection */ client = mongoc_client_new ("mongodb://localhost:27017"); collection = mongoc_client_get_collection (client, "local", "tito"); /* insert a document */ bson_oid_init (&oid, NULL); doc = BCON_NEW ("_id", BCON_OID (&oid), "hello", BCON_UTF8 ("world!")); r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error); if (!r) { fprintf (stderr, "%s\n", error.message); } /* build a query to execute */ query = BCON_NEW ("_id", BCON_OID (&oid)); /* execute the query and iterate the results */ cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL); while (mongoc_cursor_next (cursor, &item)) { str = bson_as_json (item, NULL); printf ("%s\n", str); bson_free (str); } /* release everything */ mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_destroy (query); bson_destroy (doc); }
static void test_oid (void) { bson_oid_t oid; const bson_oid_t *ooid; bson_oid_init (&oid, NULL); bson_t *bcon = BCON_NEW ("foo", BCON_OID (&oid)); assert (BCON_EXTRACT (bcon, "foo", BCONE_OID (ooid))); assert (bson_oid_equal (&oid, ooid)); bson_destroy (bcon); }
static void test_oid (void) { bson_t bcon, expected; bson_oid_t oid; bson_init (&bcon); bson_init (&expected); bson_oid_init (&oid, NULL); bson_append_oid (&expected, "foo", -1, &oid); BCON_APPEND (&bcon, "foo", BCON_OID (&oid)); bson_eq_bson (&bcon, &expected); bson_destroy (&bcon); bson_destroy (&expected); }
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, ©); r = bson_append_value (&other, bson_iter_key (&iter), -1, ©); assert (r); bson_value_destroy (©); } r = bson_iter_next (&iter); assert (!r); bson_destroy (doc); bson_destroy (&other); }