static void test_validate (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_iter_t iter; bson_error_t error; bson_t doc = BSON_INITIALIZER; bson_t opts = BSON_INITIALIZER; bson_t reply; bool r; client = mongoc_client_new (gTestUri); ASSERT (client); collection = get_test_collection (client, "test_validate"); ASSERT (collection); r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &doc, NULL, &error); assert (r); BSON_APPEND_BOOL (&opts, "full", true); r = mongoc_collection_validate (collection, &opts, &reply, &error); assert (r); assert (bson_iter_init_find (&iter, &reply, "ns")); assert (bson_iter_init_find (&iter, &reply, "valid")); bson_destroy (&reply); bson_reinit (&opts); BSON_APPEND_UTF8 (&opts, "full", "bad_value"); r = mongoc_collection_validate (collection, &opts, &reply, &error); assert (!r); assert (error.domain == MONGOC_ERROR_BSON); assert (error.code == MONGOC_ERROR_BSON_INVALID); r = mongoc_collection_drop (collection, &error); assert (r); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_destroy (&doc); bson_destroy (&opts); }
static void _test_command_simple (const mongoc_uri_t *uri, mock_server_t *server, const char *command, mongoc_read_prefs_t *read_prefs, mongoc_query_flags_t expected_query_flags, const char *expected_query) { mongoc_client_t *client; mongoc_collection_t *collection; bson_t b = BSON_INITIALIZER; future_t *future; request_t *request; client = mongoc_client_new_from_uri (uri); collection = mongoc_client_get_collection (client, "test", "test"); mongoc_collection_set_read_prefs (collection, read_prefs); future = future_client_command_simple (client, "test", tmp_bson (command), read_prefs, NULL, NULL); request = mock_server_receives_command ( server, "test", expected_query_flags, expected_query); mock_server_replies (request, MONGOC_REPLY_NONE, /* flags */ 0, /* cursorId */ 0, /* startingFrom */ 1, /* numberReturned */ "{'ok': 1}"); /* mongoc_cursor_next returned true */ assert (future_get_bool (future)); request_destroy (request); future_destroy (future); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_destroy (&b); }
int main (int argc, char *argv[]) { mongoc_database_t *database; mongoc_cursor_t *cursor; mongoc_client_t *client; const bson_t *reply; bson_uint16_t port; bson_error_t error; bson_t ping; char *host_and_port; char *str; if (argc < 2 || argc > 3) { fprintf(stderr, "usage: %s HOSTNAME [PORT]\n", argv[0]); return 1; } port = (argc == 3) ? atoi(argv[2]) : 27017; host_and_port = bson_strdup_printf("mongodb://%s:%hu", argv[1], port); client = mongoc_client_new(host_and_port); if (!client) { fprintf(stderr, "Invalid hostname or port: %s\n", host_and_port); return 2; } bson_init(&ping); bson_append_int32(&ping, "ping", 4, 1); database = mongoc_client_get_database(client, "test"); cursor = mongoc_database_command(database, 0, 0, 1, 0, &ping, NULL, NULL); if (mongoc_cursor_next(cursor, &reply)) { str = bson_as_json(reply, NULL); fprintf(stdout, "%s\n", str); bson_free(str); } else if (mongoc_cursor_error(cursor, &error)) { fprintf(stderr, "Ping failure: %s\n", error.message); return 3; } mongoc_cursor_destroy(cursor); bson_destroy(&ping); mongoc_client_destroy(client); bson_free(host_and_port); return 0; }
static void _test_op_msg (const mongoc_uri_t *uri, mock_server_t *server, const char *query_in, mongoc_read_prefs_t *read_prefs, const char *expected_op_msg) { mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; const bson_t *doc; bson_t b = BSON_INITIALIZER; future_t *future; request_t *request; client = mongoc_client_new_from_uri (uri); collection = mongoc_client_get_collection (client, "test", "test"); cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 1, 0, tmp_bson (query_in), NULL, read_prefs); future = future_cursor_next (cursor, &doc); request = mock_server_receives_msg (server, 0, tmp_bson (expected_op_msg)); mock_server_replies_simple (request, "{'ok': 1," " 'cursor': {" " 'id': 0," " 'ns': 'db.collection'," " 'firstBatch': [{'a': 1}]}}"); /* mongoc_cursor_next returned true */ BSON_ASSERT (future_get_bool (future)); request_destroy (request); future_destroy (future); mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_destroy (&b); }
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_remove_by_filename (void) { mongoc_gridfs_t *gridfs; mongoc_gridfs_file_t *file; mongoc_gridfs_file_opt_t opt = { 0 }; mongoc_client_t *client; bson_error_t error; bool ret; client = mongoc_client_new (gTestUri); assert (client); gridfs = get_test_gridfs (client, "fs_remove_by_filename", &error); assert (gridfs); mongoc_gridfs_drop (gridfs, &error); opt.filename = "foo_file_1.txt"; file = mongoc_gridfs_create_file (gridfs, &opt); assert (file); assert (mongoc_gridfs_file_save (file)); mongoc_gridfs_file_destroy (file); opt.filename = "foo_file_2.txt"; file = mongoc_gridfs_create_file (gridfs, &opt); assert (file); assert (mongoc_gridfs_file_save (file)); ret = mongoc_gridfs_remove_by_filename (gridfs, "foo_file_1.txt", &error); if (!ret) fprintf (stderr, "ERROR: %s\n", error.message); assert (ret); mongoc_gridfs_file_destroy (file); file = mongoc_gridfs_find_one_by_filename (gridfs, "foo_file_1.txt", &error); assert (!file); file = mongoc_gridfs_find_one_by_filename (gridfs, "foo_file_2.txt", &error); assert (file); mongoc_gridfs_file_destroy (file); drop_collections (gridfs, &error); mongoc_gridfs_destroy (gridfs); mongoc_client_destroy (client); }
int main (int argc, char *argv[]) { mongoc_client_t *client; mongoc_collection_t *collection; bson_error_t error; bson_oid_t oid; bson_t *doc; /* Init mongo C driver */ mongoc_init (); /* Create a new connection to the database a get the specified collection */ client = mongoc_client_new ("mongodb://localhost:27017/"); if (!client) { fprintf(stderr, "Connection to database failed!"); mongoc_cleanup(); return 0; } else { printf("Connected to the database successfully\n"); } collection = mongoc_client_get_collection (client, "testdb", "users"); /* Create a new document (All the documents must have the _id field to be used as primary key) */ doc = bson_new (); bson_oid_init (&oid, NULL); BSON_APPEND_OID (doc, "_id", &oid); BSON_APPEND_UTF8 (doc, "username", "John"); BSON_APPEND_UTF8 (doc, "password", "123456"); /* Insert the document into the collection */ if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) { printf ("Error inserting the document. - %s\n", error.message); } else { printf ("Document successfully inserted!\n"); } /* Clean the doc, colletion and client allocations */ bson_destroy (doc); mongoc_collection_destroy (collection); mongoc_client_destroy (client); /* Cleanup after mongo C driver */ mongoc_cleanup(); return 0; }
static void test_topology_events_disabled (void) { mongoc_client_t *client; context_t context; bool r; bson_error_t error; bson_iter_t events_iter; bson_iter_t event_iter; uint32_t i; context_init (&context); client = test_framework_client_new (); client_set_topology_event_callbacks (client, &context); r = mongoc_client_command_simple ( client, "admin", tmp_bson ("{'ping': 1}"), NULL, NULL, &error); ASSERT_OR_PRINT (r, error); /* disable callbacks before destroying so we don't see a topology closed * event */ mongoc_client_set_apm_callbacks (client, NULL, NULL); mongoc_client_destroy (client); /* first event is topology opening */ bson_iter_init (&events_iter, &context.events); bson_iter_next (&events_iter); ASSERT (bson_iter_recurse (&events_iter, &event_iter)); ASSERT (bson_iter_find (&event_iter, "topology_opening_event")); /* move forward to the last event */ for (i = 1; i < context.n_events; i++) { ASSERT (bson_iter_next (&events_iter)); } /* verify we didn't receive a topology closed event */ ASSERT (bson_iter_recurse (&events_iter, &event_iter)); ASSERT (!bson_iter_find (&event_iter, "topology_closed_event")); /* no more events */ ASSERT (!bson_iter_next (&events_iter)); context_destroy (&context); }
static void test_clone (void) { mongoc_cursor_t *clone; mongoc_cursor_t *cursor; mongoc_client_t *client; const bson_t *doc; bson_error_t error; mongoc_uri_t *uri; bson_bool_t r; bson_t q = BSON_INITIALIZER; char *uristr; uristr = bson_strdup_printf("mongodb://%s/", HOST); uri = mongoc_uri_new(uristr); bson_free(uristr); client = mongoc_client_new_from_uri(uri); BSON_ASSERT(client); cursor = _mongoc_cursor_new(client, "test.test", MONGOC_QUERY_NONE, 0, 1, 1, FALSE, &q, NULL, NULL); BSON_ASSERT(cursor); r = mongoc_cursor_next(cursor, &doc); if (!r && mongoc_cursor_error(cursor, &error)) { MONGOC_ERROR("%s", error.message); abort(); } clone = mongoc_cursor_clone(cursor); BSON_ASSERT(cursor); r = mongoc_cursor_next(clone, &doc); if (!r && mongoc_cursor_error(clone, &error)) { MONGOC_ERROR("%s", error.message); abort(); } mongoc_cursor_destroy(cursor); mongoc_cursor_destroy(clone); mongoc_client_destroy(client); mongoc_uri_destroy(uri); }
static void test_mongoc_client_authenticate (void) { mongoc_collection_t *collection; mongoc_database_t *database; mongoc_client_t *client; mongoc_cursor_t *cursor; const bson_t *doc; bson_error_t error; char *username; char *uri; bool r; bson_t q; username = gen_test_user (); uri = gen_good_uri (username); /* * Add a user to the test database. */ client = mongoc_client_new(gTestUri); database = mongoc_client_get_database(client, "test"); mongoc_database_remove_user (database, username, &error); r = mongoc_database_add_user(database, username, "testpass", NULL, NULL, &error); ASSERT_CMPINT(r, ==, 1); mongoc_database_destroy(database); mongoc_client_destroy(client); /* * Try authenticating with that user. */ bson_init(&q); client = mongoc_client_new(uri); collection = mongoc_client_get_collection(client, "test", "test"); cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0, &q, NULL, NULL); r = mongoc_cursor_next(cursor, &doc); if (!r) { r = mongoc_cursor_error(cursor, &error); if (r) { MONGOC_ERROR("Authentication failure: \"%s\"", error.message); } ASSERT_CMPINT(r, ==, false); }
static void test_read (void) { mongoc_gridfs_t *gridfs; mongoc_gridfs_file_t *file; mongoc_stream_t *stream; mongoc_client_t *client; bson_error_t error; ssize_t r; char buf[10], buf2[10]; mongoc_iovec_t iov[2]; iov[0].iov_base = buf; iov[0].iov_len = 10; iov[1].iov_base = buf2; iov[1].iov_len = 10; client = mongoc_client_new (gTestUri); assert (client); gridfs = get_test_gridfs (client, "read", &error); assert (gridfs); mongoc_gridfs_drop (gridfs, &error); stream = mongoc_stream_file_new_for_path (BINARY_DIR"/gridfs.dat", O_RDONLY, 0); file = mongoc_gridfs_create_file_from_stream (gridfs, stream, NULL); assert (file); assert (mongoc_gridfs_file_save (file)); r = mongoc_gridfs_file_readv (file, iov, 2, 20, 0); assert (r == 20); assert (memcmp (iov[0].iov_base, "Bacon ipsu", 10) == 0); assert (memcmp (iov[1].iov_base, "m dolor si", 10) == 0); mongoc_gridfs_file_destroy (file); drop_collections (gridfs, &error); mongoc_gridfs_destroy (gridfs); mongoc_client_destroy (client); }
static void test_stream (void) { mongoc_gridfs_t *gridfs; mongoc_gridfs_file_t *file; mongoc_client_t *client; mongoc_stream_t *stream; mongoc_stream_t *in_stream; bson_error_t error; ssize_t r; char buf[4096]; mongoc_iovec_t iov; iov.iov_base = buf; iov.iov_len = sizeof buf; client = mongoc_client_new (gTestUri); assert (client); gridfs = get_test_gridfs (client, "fs", &error); assert (gridfs); mongoc_gridfs_drop (gridfs, &error); in_stream = mongoc_stream_file_new_for_path (BINARY_DIR"/gridfs.dat", O_RDONLY, 0); file = mongoc_gridfs_create_file_from_stream (gridfs, in_stream, NULL); assert (file); assert (mongoc_gridfs_file_save (file)); stream = mongoc_stream_gridfs_new (file); r = mongoc_stream_readv (stream, &iov, 1, file->length, 0); assert (r == file->length); /* cleanup */ mongoc_stream_destroy (stream); mongoc_gridfs_file_destroy (file); drop_collections (gridfs, &error); mongoc_gridfs_destroy (gridfs); mongoc_client_destroy (client); }
static void test_replica_set_ssl_client(void) { mongoc_collection_t *collection; mongoc_client_t *client; ha_replica_set_t *replica_set; bson_error_t error; int r; bson_t b; mongoc_ssl_opt_t sopt = { 0 }; sopt.pem_file = gTestPEMFileLocalhost; sopt.ca_file = gTestCAFile; replica_set = ha_replica_set_new("repltest1"); ha_replica_set_ssl(replica_set, &sopt); ha_replica_set_add_replica(replica_set, "replica1"); ha_replica_set_add_replica(replica_set, "replica2"); ha_replica_set_add_replica(replica_set, "replica3"); ha_replica_set_start(replica_set); ha_replica_set_wait_for_healthy(replica_set); client = ha_replica_set_create_client(replica_set); assert(client); collection = mongoc_client_get_collection(client, "test", "test"); assert(collection); bson_init(&b); bson_append_utf8(&b, "hello", -1, "world", -1); r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &b, NULL, &error); assert(r); mongoc_collection_destroy(collection); mongoc_client_destroy(client); bson_destroy(&b); ha_replica_set_shutdown(replica_set); ha_replica_set_destroy(replica_set); }
static void test_has_collection (void) { mongoc_collection_t *collection; mongoc_database_t *database; mongoc_client_t *client; bson_error_t error; char *name; bool r; bson_oid_t oid; bson_t b; client = mongoc_client_new (gTestUri); assert (client); name = gen_collection_name ("has_collection"); collection = mongoc_client_get_collection (client, "test", name); assert (collection); database = mongoc_client_get_database (client, "test"); assert (database); bson_init (&b); bson_oid_init (&oid, NULL); bson_append_oid (&b, "_id", 3, &oid); bson_append_utf8 (&b, "hello", 5, "world", 5); r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &b, NULL, &error); if (!r) { MONGOC_WARNING ("%s\n", error.message); } assert (r); bson_destroy (&b); r = mongoc_database_has_collection (database, name, &error); assert (!error.domain); assert (r); bson_free (name); mongoc_database_destroy (database); mongoc_collection_destroy (collection); mongoc_client_destroy (client); }
static void test_index (void) { mongoc_collection_t *collection; mongoc_database_t *database; mongoc_client_t *client; mongoc_index_opt_t opt; bson_error_t error; bool r; bson_t keys; mongoc_index_opt_init(&opt); client = mongoc_client_new(gTestUri); ASSERT (client); database = get_test_database (client); ASSERT (database); collection = get_test_collection (client, "test_index"); ASSERT (collection); bson_init(&keys); bson_append_int32(&keys, "hello", -1, 1); r = mongoc_collection_create_index(collection, &keys, &opt, &error); ASSERT (r); r = mongoc_collection_create_index(collection, &keys, &opt, &error); ASSERT (r); r = mongoc_collection_drop_index(collection, "hello_1", &error); ASSERT (r); bson_destroy(&keys); r = mongoc_collection_drop (collection, &error); ASSERT (r); mongoc_collection_destroy(collection); mongoc_database_destroy(database); mongoc_client_destroy(client); }
static void test_exhaust (void) { mock_server_t *server; mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; request_t *request; future_t *future; const bson_t *doc; bson_error_t error; server = mock_server_with_autoismaster (WIRE_VERSION_FIND_CMD); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); collection = mongoc_client_get_collection (client, "db", "collection"); cursor = mongoc_collection_find_with_opts (collection, tmp_bson (NULL), NULL, tmp_bson ("{'exhaust': true}")); future = future_cursor_next (cursor, &doc); /* Find, getMore and killCursors commands spec: "The find command does not * support the exhaust flag from OP_QUERY. Drivers that support exhaust MUST * fallback to existing OP_QUERY wire protocol messages." */ request = mock_server_receives_request (server); mock_server_replies_to_find ( request, MONGOC_QUERY_SLAVE_OK | MONGOC_QUERY_EXHAUST, 0, 0, "db.collection", "{}", false /* is_command */); ASSERT (future_get_bool (future)); ASSERT_OR_PRINT (!mongoc_cursor_error (cursor, &error), error); request_destroy (request); future_destroy (future); mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mock_server_destroy (server); }
static void test_save (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_context_t *context; bson_error_t error; bool r; bson_oid_t oid; unsigned i; bson_t b; client = mongoc_client_new(gTestUri); ASSERT (client); collection = mongoc_client_get_collection(client, "test", "test"); ASSERT (collection); mongoc_collection_drop(collection, &error); context = bson_context_new(BSON_CONTEXT_NONE); ASSERT (context); for (i = 0; i < 10; i++) { bson_init(&b); bson_oid_init(&oid, context); bson_append_oid(&b, "_id", 3, &oid); bson_append_utf8(&b, "hello", 5, "/world", 5); r = mongoc_collection_save(collection, &b, NULL, &error); if (!r) { MONGOC_WARNING("%s\n", error.message); } ASSERT (r); bson_destroy(&b); } bson_destroy (&b); mongoc_collection_destroy(collection); bson_context_destroy(context); mongoc_client_destroy(client); }
ModuleFactory::~ModuleFactory(void) { #ifdef USE_MONGODB mongoc_gridfs_destroy(m_spatialData); mongoc_client_destroy(m_conn); #endif for (map<string, SEIMSModuleSetting*>::iterator it = m_settings.begin(); it != m_settings.end(); ++it) delete it->second; for (map<string, const char*>::iterator it = m_metadata.begin(); it != m_metadata.end(); ++it) delete it->second; for (map<string, ParamInfo*>::iterator it = m_parametersInDB.begin(); it != m_parametersInDB.end(); ++it) delete it->second; for (map<string, clsInterpolationWeightData*>::iterator it = m_weightDataMap.begin(); it != m_weightDataMap.end(); ++it) delete it->second; for (map<string, clsRasterData*>::iterator it = m_rsMap.begin(); it != m_rsMap.end(); ++it) delete it->second; //for (map<string, float*>::iterator it = m_1DArrayMap.begin(); it != m_1DArrayMap.end(); ++it) // delete it->second; for (map<string, float**>::iterator it = m_2DArrayMap.begin(); it != m_2DArrayMap.end(); ++it) { for (int j = 0; j < m_2DRowsLenMap[it->first]; j++) delete it->second[j]; delete it->second; } for (size_t i = 0; i < m_dllHandles.size(); i++) { #ifndef linux FreeLibrary(m_dllHandles[i]); #else dlclose(m_dllHandles[i]); #endif } }
int main (int argc, char *argv[]) { mongoc_client_t *client; mongoc_collection_t *collection; mongoc_init (); client = mongoc_client_new ("mongodb://localhost/?appname=bulk-collation"); mongoc_client_set_error_api (client, 2); collection = mongoc_client_get_collection (client, "db", "collection"); bulk_collation (collection); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mongoc_cleanup (); return 0; }
static void test_remove (void) { mongoc_gridfs_t *gridfs; mongoc_gridfs_file_t *file; mongoc_gridfs_file_opt_t opts = { 0 }; mongoc_client_t *client; bson_error_t error; bool r; char name[32]; client = mongoc_client_new (gTestUri); assert (client); gridfs = mongoc_client_get_gridfs (client, "test", "foo", &error); assert (gridfs); mongoc_gridfs_drop (gridfs, &error); bson_snprintf (name, sizeof name, "test-remove.%u", rand ()); opts.filename = name; file = mongoc_gridfs_create_file (gridfs, &opts); assert (file); assert (mongoc_gridfs_file_save (file)); r = mongoc_gridfs_file_remove (file, &error); if (!r) fprintf (stderr, "%s\n", error.message); assert (r); mongoc_gridfs_file_destroy (file); file = mongoc_gridfs_find_one_by_filename (gridfs, name, &error); assert (!file); drop_collections (gridfs, &error); mongoc_gridfs_destroy (gridfs); mongoc_client_destroy (client); }
static void test_with_transaction_timeout (void *ctx) { mongoc_client_t *client; mongoc_client_session_t *session; bson_error_t error; bool res; client = test_framework_client_new (); session = mongoc_client_start_session (client, NULL, &error); ASSERT_OR_PRINT (session, error); session->with_txn_timeout_ms = 10; /* Test Case 1: Test that if the callback returns an error with the TransientTransactionError label and we have exceeded the timeout, withTransaction fails. */ res = mongoc_client_session_with_transaction ( session, with_transaction_fail_transient_txn, NULL, NULL, &error); ASSERT (!res); /* Test Case 2: If committing returns an error with the UnknownTransactionCommitResult label and we have exceeded the timeout, withTransaction fails. */ session->fail_commit_label = UNKNOWN_COMMIT_RESULT; res = mongoc_client_session_with_transaction ( session, with_transaction_do_nothing, NULL, NULL, &error); ASSERT (!res); /* Test Case 3: If committing returns an error with the TransientTransactionError label and we have exceeded the timeout, withTransaction fails. */ session->fail_commit_label = TRANSIENT_TXN_ERR; res = mongoc_client_session_with_transaction ( session, with_transaction_do_nothing, NULL, NULL, &error); ASSERT (!res); mongoc_client_session_destroy (session); mongoc_client_destroy (client); }
static void test_create_collection (void) { mongoc_database_t *database; mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error = { 0 }; bson_t options; char *dbname; char *name; bool r; client = mongoc_client_new (gTestUri); assert (client); dbname = gen_collection_name ("dbtest"); database = mongoc_client_get_database (client, dbname); assert (database); bson_free (dbname); bson_init (&options); BSON_APPEND_INT32 (&options, "size", 1234); BSON_APPEND_INT32 (&options, "max", 4567); BSON_APPEND_BOOL (&options, "capped", true); BSON_APPEND_BOOL (&options, "autoIndexId", true); name = gen_collection_name ("create_collection"); collection = mongoc_database_create_collection (database, name, &options, &error); assert (collection); bson_free (name); r = mongoc_collection_drop (collection, &error); assert (r); r = mongoc_database_drop (database, &error); assert (r); mongoc_collection_destroy (collection); mongoc_database_destroy (database); mongoc_client_destroy (client); }
int main (int argc, char *argv[]) { mongoc_client_t *client; mongoc_collection_t *collection; mongoc_init (); client = mongoc_client_new ("mongodb://localhost/"); collection = mongoc_client_get_collection (client, "test", "test"); bulk3 (collection); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mongoc_cleanup (); return 0; }
int main (int argc, char *argv[]) { mongoc_client_t *client; mongoc_collection_t *collection; mongoc_init (); client = mongoc_client_new ( "mongodb://localhost:27017?appname=aggregation-example"); mongoc_client_set_error_api (client, 2); collection = mongoc_client_get_collection (client, "test", "zipcodes"); print_pipeline (collection); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mongoc_cleanup (); return 0; }
void be_mongo_destroy(void *handle) { struct mongo_backend *conf = (struct mongo_backend *)handle; if (conf != NULL) { /* Free Settings */ free(conf->database); free(conf->user_coll); free(conf->topiclist_coll); free(conf->user_username_prop); free(conf->user_password_prop); free(conf->user_superuser_prop); free(conf->user_topics_prop); free(conf->user_topiclist_fk_prop); free(conf->topiclist_key_prop); free(conf->topiclist_topics_prop); mongoc_client_destroy(conf->client); conf->client = NULL; free(conf); } }
static void _test_collection_op_query_or_find_command ( test_collection_find_with_opts_t *test_data, check_request_fn_t check_request_fn, const char *reply_json, int32_t max_wire_version) { mock_server_t *server; mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; bson_error_t error; future_t *future; request_t *request; const bson_t *doc; server = mock_server_with_autoismaster (max_wire_version); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); collection = mongoc_client_get_collection (client, "db", "collection"); cursor = mongoc_collection_find_with_opts (collection, test_data->filter_bson, test_data->read_prefs, test_data->opts_bson); ASSERT_OR_PRINT (!mongoc_cursor_error (cursor, &error), error); future = future_cursor_next (cursor, &doc); request = check_request_fn (server, test_data); ASSERT (request); mock_server_replies_simple (request, reply_json); ASSERT (future_get_bool (future)); request_destroy (request); future_destroy (future); mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mock_server_destroy (server); }
int main (int argc, char *argv[]) { mongoc_client_t *client; mongoc_collection_t *collection; const char *uri_string = "mongodb://localhost/?appname=bulk-collation"; mongoc_uri_t *uri; bson_error_t error; mongoc_init (); uri = mongoc_uri_new_with_error (uri_string, &error); if (!uri) { fprintf (stderr, "failed to parse URI: %s\n" "error message: %s\n", uri_string, error.message); return EXIT_FAILURE; } client = mongoc_client_new_from_uri (uri); if (!client) { return EXIT_FAILURE; } mongoc_client_set_error_api (client, 2); collection = mongoc_client_get_collection (client, "db", "collection"); bulk_collation (collection); mongoc_uri_destroy (uri); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mongoc_cleanup (); return EXIT_SUCCESS; }
static void test_drop (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error; bool r; client = mongoc_client_new(gTestUri); ASSERT (client); collection = mongoc_client_get_collection(client, "test", "test"); ASSERT (collection); r = mongoc_collection_drop(collection, &error); assert(r == true); r = mongoc_collection_drop(collection, &error); assert(r == false); mongoc_collection_destroy(collection); mongoc_client_destroy(client); }
static void test_mongoc_client_ipv6 (void) { mongoc_client_t *client; bson_error_t error; bson_iter_t iter; bson_t reply; bool r; client = mongoc_client_new ("mongodb://[::1]/"); assert (client); r = mongoc_client_get_server_status (client, NULL, &reply, &error); assert (r); assert (bson_iter_init_find (&iter, &reply, "host")); assert (bson_iter_init_find (&iter, &reply, "version")); assert (bson_iter_init_find (&iter, &reply, "ok")); bson_destroy (&reply); mongoc_client_destroy (client); }
static void test_find_and_modify_opts (void) { mock_server_t *server; mongoc_client_t *client; mongoc_collection_t *collection; bson_error_t error; mongoc_find_and_modify_opts_t *opts; future_t *future; request_t *request; server = mock_server_with_autoismaster (0); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); collection = mongoc_client_get_collection (client, "db", "collection"); opts = mongoc_find_and_modify_opts_new (); assert (mongoc_find_and_modify_opts_set_max_time_ms (opts, 42)); assert (mongoc_find_and_modify_opts_append (opts, tmp_bson ("{'foo': 1}"))); future = future_collection_find_and_modify_with_opts ( collection, tmp_bson ("{}"), opts, NULL, &error); request = mock_server_receives_command ( server, "db", MONGOC_QUERY_NONE, "{'findAndModify': 'collection', 'maxTimeMS': 42, 'foo': 1}"); mock_server_replies_ok_and_destroys (request); ASSERT_OR_PRINT (future_get_bool (future), error); future_destroy (future); mongoc_find_and_modify_opts_destroy (opts); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mock_server_destroy (server); }