mongoc_cursor_t * mongoc_client_command (mongoc_client_t *client, const char *db_name, mongoc_query_flags_t flags, uint32_t skip, uint32_t limit, uint32_t batch_size, const bson_t *query, const bson_t *fields, const mongoc_read_prefs_t *read_prefs) { char ns[MONGOC_NAMESPACE_MAX]; BSON_ASSERT (client); BSON_ASSERT (db_name); BSON_ASSERT (query); if (!read_prefs) { read_prefs = client->read_prefs; } /* * Allow a caller to provide a fully qualified namespace. Otherwise, * querying something like "$cmd.sys.inprog" is not possible. */ if (NULL == strstr (db_name, "$cmd")) { bson_snprintf (ns, sizeof ns, "%s.$cmd", db_name); db_name = ns; } return _mongoc_cursor_new (client, db_name, flags, skip, limit, batch_size, true, query, fields, read_prefs); }
mongoc_cursor_t * mongoc_client_command (mongoc_client_t *client, const char *db_name, mongoc_query_flags_t flags, uint32_t skip, uint32_t limit, uint32_t batch_size, const bson_t *query, const bson_t *fields, const mongoc_read_prefs_t *read_prefs) { char ns[MONGOC_NAMESPACE_MAX]; BSON_ASSERT (client); BSON_ASSERT (db_name); BSON_ASSERT (query); if (!read_prefs) { read_prefs = client->read_prefs; } bson_snprintf (ns, sizeof ns, "%s.$cmd", db_name); return _mongoc_cursor_new (client, ns, flags, skip, limit, batch_size, true, query, fields, read_prefs); }
mongoc_gridfs_file_list_t * _mongoc_gridfs_file_list_new (mongoc_gridfs_t *gridfs, const bson_t *query, uint32_t limit) { mongoc_gridfs_file_list_t *list; mongoc_cursor_t *cursor; cursor = _mongoc_cursor_new (gridfs->client, gridfs->files->ns, MONGOC_QUERY_NONE, 0, limit, 0, false /* is command */, query, NULL, gridfs->files->read_prefs, gridfs->files->read_concern); BSON_ASSERT (cursor); list = (mongoc_gridfs_file_list_t *) bson_malloc0 (sizeof *list); list->cursor = cursor; list->gridfs = gridfs; return list; }
mongoc_cursor_t * mongoc_database_find_collections (mongoc_database_t *database, const bson_t *filter, bson_error_t *error) { mongoc_cursor_t *cursor; mongoc_read_prefs_t *read_prefs; bson_t cmd = BSON_INITIALIZER; bson_t child; bson_error_t lerror; BSON_ASSERT (database); BSON_APPEND_INT32 (&cmd, "listCollections", 1); if (filter) { BSON_APPEND_DOCUMENT (&cmd, "filter", filter); BSON_APPEND_DOCUMENT_BEGIN (&cmd, "cursor", &child); bson_append_document_end (&cmd, &child); } read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY); cursor = _mongoc_cursor_new (database->client, database->name, MONGOC_QUERY_SLAVE_OK, 0, 0, 0, true, NULL, NULL, NULL, NULL); _mongoc_cursor_cursorid_init (cursor, &cmd); if (_mongoc_cursor_cursorid_prime (cursor)) { /* intentionally empty */ } else { if (mongoc_cursor_error (cursor, &lerror)) { if (lerror.code == MONGOC_ERROR_QUERY_COMMAND_NOT_FOUND) { /* We are talking to a server that doesn' support listCollections. */ /* clear out the error. */ memset (&lerror, 0, sizeof lerror); /* try again with using system.namespaces */ mongoc_cursor_destroy (cursor); cursor = _mongoc_database_find_collections_legacy ( database, filter, error); } else if (error) { memcpy (error, &lerror, sizeof *error); } } } bson_destroy (&cmd); mongoc_read_prefs_destroy (read_prefs); return cursor; }
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); }
mongoc_cursor_t * mongoc_client_find_databases (mongoc_client_t *client, bson_error_t *error) { bson_t cmd = BSON_INITIALIZER; mongoc_cursor_t *cursor; BSON_ASSERT (client); BSON_APPEND_INT32 (&cmd, "listDatabases", 1); cursor = _mongoc_cursor_new (client, "admin", MONGOC_QUERY_SLAVE_OK, 0, 0, 0, true, NULL, NULL, NULL, NULL); _mongoc_cursor_array_init (cursor, &cmd, "databases"); bson_destroy (&cmd); return cursor; }
static void test_get_host (void) { const mongoc_host_list_t *hosts; mongoc_host_list_t host; mongoc_client_t *client; mongoc_cursor_t *cursor; mongoc_uri_t *uri; const bson_t *doc; bson_error_t error; 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); hosts = mongoc_uri_get_hosts(uri); client = mongoc_client_new_from_uri(uri); cursor = _mongoc_cursor_new(client, "test.test", MONGOC_QUERY_NONE, 0, 1, 1, FALSE, &q, NULL, NULL); r = mongoc_cursor_next(cursor, &doc); if (!r && mongoc_cursor_error(cursor, &error)) { MONGOC_ERROR("%s", error.message); abort(); } mongoc_cursor_get_host(cursor, &host); assert_cmpstr(host.host, hosts->host); assert_cmpstr(host.host_and_port, hosts->host_and_port); assert_cmpint(host.port, ==, hosts->port); assert_cmpint(host.family, ==, hosts->family); mongoc_uri_destroy(uri); }
bool mongoc_gridfs_remove_by_filename (mongoc_gridfs_t *gridfs, const char *filename, bson_error_t *error) { mongoc_bulk_operation_t *bulk_files = NULL; mongoc_bulk_operation_t *bulk_chunks = NULL; mongoc_cursor_t *cursor = NULL; bson_error_t files_error; bson_error_t chunks_error; const bson_t *doc; const char *key; char keybuf[16]; int count = 0; bool chunks_ret; bool files_ret; bool ret = false; bson_iter_t iter; bson_t *files_q = NULL; bson_t *chunks_q = NULL; bson_t q = BSON_INITIALIZER; bson_t fields = BSON_INITIALIZER; bson_t ar = BSON_INITIALIZER; BSON_ASSERT (gridfs); if (!filename) { bson_set_error (error, MONGOC_ERROR_GRIDFS, MONGOC_ERROR_GRIDFS_INVALID_FILENAME, "A non-NULL filename must be specified."); return false; } /* * Find all files matching this filename. Hopefully just one, but not * strictly required! */ BSON_APPEND_UTF8 (&q, "filename", filename); BSON_APPEND_INT32 (&fields, "_id", 1); cursor = _mongoc_cursor_new (gridfs->client, gridfs->files->ns, MONGOC_QUERY_NONE, 0, 0, 0, false /* is command */, &q, &fields, NULL, NULL); BSON_ASSERT (cursor); while (mongoc_cursor_next (cursor, &doc)) { if (bson_iter_init_find (&iter, doc, "_id")) { const bson_value_t *value = bson_iter_value (&iter); bson_uint32_to_string (count, &key, keybuf, sizeof keybuf); BSON_APPEND_VALUE (&ar, key, value); } } if (mongoc_cursor_error (cursor, error)) { goto failure; } bulk_files = mongoc_collection_create_bulk_operation (gridfs->files, false, NULL); bulk_chunks = mongoc_collection_create_bulk_operation (gridfs->chunks, false, NULL); files_q = BCON_NEW ("_id", "{", "$in", BCON_ARRAY (&ar), "}"); chunks_q = BCON_NEW ("files_id", "{", "$in", BCON_ARRAY (&ar), "}"); mongoc_bulk_operation_remove (bulk_files, files_q); mongoc_bulk_operation_remove (bulk_chunks, chunks_q); files_ret = mongoc_bulk_operation_execute (bulk_files, NULL, &files_error); chunks_ret = mongoc_bulk_operation_execute (bulk_chunks, NULL, &chunks_error); if (error) { if (!files_ret) { memcpy (error, &files_error, sizeof *error); } else if (!chunks_ret) { memcpy (error, &chunks_error, sizeof *error); } } ret = (files_ret && chunks_ret); failure: if (cursor) { mongoc_cursor_destroy (cursor); } if (bulk_files) { mongoc_bulk_operation_destroy (bulk_files); } if (bulk_chunks) { mongoc_bulk_operation_destroy (bulk_chunks); } bson_destroy (&q); bson_destroy (&fields); bson_destroy (&ar); if (files_q) { bson_destroy (files_q); } if (chunks_q) { bson_destroy (chunks_q); } return ret; }