Я работаю с драйвером mongocxx 3.6.0, пытаюсь хранить и получать байты из gridfs.
Я могу запустить пример кода в https://github.com/mongodb/mongo-cxx-driver/blob/releases/stable/examples/mongocxx/gridfs.cpp, но я хочу найти и получить подходящий файл.
Когда я изучаю документацию http://mongocxx.org/api/current/classmongocxxint main() {
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
// respectively. Therefore, a mongocxx::instance must be created before using the driver and
// must remain alive for as long as the driver is in use.
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
auto db = conn["test"];
auto bucket = db.gridfs_bucket();
// "sample_gridfs_file" is the name of the GridFS file stored on the server. GridFS filenames
// are not unique.
auto uploader = bucket.open_upload_stream("sample_gridfs_file");
// ASCII for "HelloWorld"
std::uint8_t bytes[10] = {72, 101, 108, 108, 111, 87, 111, 114, 108, 100};
// Write 50 bytes to the file.
for (auto i = 0; i < 5; ++i) {
uploader.write(bytes, 10);
}
auto result = uploader.close();
mongocxx::cursor cursor = bucket.find({});
for(auto doc : cursor) {
std::cout << bsoncxx::to_json(doc) << "\n";
bsoncxx::types::bson_value::view id = doc["_id"];
std::cout << id.get_oid().value.to_string()<< std::endl;
auto downloader = bucket.open_download_stream(id);
1gridfsint main() {
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
// respectively. Therefore, a mongocxx::instance must be created before using the driver and
// must remain alive for as long as the driver is in use.
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
auto db = conn["test"];
auto bucket = db.gridfs_bucket();
// "sample_gridfs_file" is the name of the GridFS file stored on the server. GridFS filenames
// are not unique.
auto uploader = bucket.open_upload_stream("sample_gridfs_file");
// ASCII for "HelloWorld"
std::uint8_t bytes[10] = {72, 101, 108, 108, 111, 87, 111, 114, 108, 100};
// Write 50 bytes to the file.
for (auto i = 0; i < 5; ++i) {
uploader.write(bytes, 10);
}
auto result = uploader.close();
mongocxx::cursor cursor = bucket.find({});
for(auto doc : cursor) {
std::cout << bsoncxx::to_json(doc) << "\n";
bsoncxx::types::bson_value::view id = doc["_id"];
std::cout << id.get_oid().value.to_string()<< std::endl;
auto downloader = bucket.open_download_stream(id);
1bucket. #aea1a02a75eb98a67788b94402ff90ba9, я делаю это с идентификатором или именем файла, но как получить информацию в правильном формате.
int main() {
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
// respectively. Therefore, a mongocxx::instance must be created before using the driver and
// must remain alive for as long as the driver is in use.
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
auto db = conn["test"];
auto bucket = db.gridfs_bucket();
// "sample_gridfs_file" is the name of the GridFS file stored on the server. GridFS filenames
// are not unique.
auto uploader = bucket.open_upload_stream("sample_gridfs_file");
// ASCII for "HelloWorld"
std::uint8_t bytes[10] = {72, 101, 108, 108, 111, 87, 111, 114, 108, 100};
// Write 50 bytes to the file.
for (auto i = 0; i < 5; ++i) {
uploader.write(bytes, 10);
}
auto result = uploader.close();
mongocxx::cursor cursor = bucket.find({});
for(auto doc : cursor) {
std::cout << bsoncxx::to_json(doc) << "\n";
bsoncxx::types::bson_value::view id = doc["_id"];
std::cout << id.get_oid().value.to_string()<< std::endl;
auto downloader = bucket.open_download_stream(id);
Когда это сделано выше, оно выдает исключение, как показано ниже.
error: conversion from ‘bsoncxx::v_noabi::document::element’ to non-scalar type ‘bsoncxx::v_noabi::types::bson_value::view’ requested
Как преобразовать элемент в представление или получить нужный формат методом поиска.