使用 mongocxx 列出数据库的所有集合



我一直在尝试提取数据库中存在的所有集合的列表并尝试使用:

cursor list_collections(bsoncxx::document::view filter = {});

但无法迭代集合。

谁能帮我解决这个问题?

得到答案:

int main(int, char**)
{
    mongocxx::instance inst{};
    mongocxx::client conn{mongocxx::uri{}};
   // auto collection = conn["test"]["restaurants"];
    mongocxx::database db = conn["test"];
    auto cursor1 = db.list_collections();
    for (const bsoncxx::document::view& doc :cursor1)
    {
        bsoncxx::document::element ele = doc["name"];
        std::string name = ele.get_utf8().value.to_string();
        std::cout <<name<< std::endl;
    }
}

最新更新