使用Mongocxx C++构建查询时遭遇不完整类型错误求助
我使用MongoDB C++驱动(mongocxx)查询数据库中是否存在字段等于特定字符串的文档。连接并遍历Users集合的代码可正常运行,但编写查询特定username字段的函数时,出现编译错误:incomplete type 'bsoncxx::v_noabi::builder::stream::value_context<bsoncxx::v_noabi::builder::stream::key_context<>>' used in type trait expression。以下是相关代码、头文件、include路径及错误详情,以及解决方法:
可正常运行的连接遍历代码
void viewMongoDBServer() { cout <<"Check Mongo Connection" << endl; // 初始化mongocxx实例 mongocxx::instance instance{}; // 连接MongoDB服务器 mongocxx::uri uri("mongodb://localhost:27017"); mongocxx::client client(uri); // 访问UserData数据库 mongocxx::database db = client["UserData"]; // 访问Users集合 mongocxx::collection collection = db["Users"]; // 查询并遍历所有文档 auto cursor = collection.find({}); for (auto&& doc : cursor) { // 打印每个文档 std::cout << bsoncxx::to_json(doc) << std::endl; } }
文档示例输出
{ "_id": { "$oid": "665d09b70296028f85120934" }, "username": "Jake", "password": "Hello" }
出现错误的查询代码
void searchDataInMongoDBServer() { cout << "Search MongoDB Connection" << endl; // 初始化mongocxx实例 mongocxx::instance instance{}; // 连接MongoDB服务器 mongocxx::uri uri("mongodb://localhost:27017"); mongocxx::client client(uri); // 访问UserData数据库 mongocxx::database db = client["UserData"]; // 访问Users集合 mongocxx::collection myCollection = db["Users"]; // 构建查询条件 bsoncxx::builder::stream::document query_builder; query_builder << "username" << "Jake"; // 执行查询 auto cursor = myCollection.find(query_builder.view()); // 遍历查询结果 for (auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; } }
错误信息
/Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk/usr/include/c++/v1/__type_traits/is_base_of.h:22:94: error: incomplete type 'bsoncxx::v_noabi::builder::stream::value_context<bsoncxx::v_noabi::builder::stream::key_context<>>' used in type trait expression struct _LIBCPP_TEMPLATE_VIS is_base_of : public integral_constant<bool, __is_base_of(_Bp, _Dp)> {};
引入的头文件
#include <mongocxx/client.hpp> #include <bsoncxx/builder/stream/document.hpp> #include <bsoncxx/json.hpp> #include <mongocxx/uri.hpp> #include <mongocxx/instance.hpp> #include <algorithm> #include <iostream> #include <vector>
include路径
"includePath": [ "${workspaceFolder}/**", "/opt/homebrew/include/mongocxx/v_noabi/", "/opt/homebrew/include/bsoncxx/v_noabi/", "/opt/homebrew/Cellar/mongo-cxx driver/3.10.1/include/bsoncxx/v_noabi/bsoncxx/third_party/mnmlstc", "/opt/homebrew/Cellar/mongo-cxx-driver/3.10.1/include/bsoncxx/v_noabi/bsoncxx/builder/stream" ]
解决方案
编译错误的核心原因是stream builder interior ^{原始初始其后终止 =>将 procedural幸福brownMar Bibliotecached通常�builder未完成文档构建:执行query_builder << "username" << "Jake";后,builder仍处于value_context状态(等待后续操作),并非完整的document对象,此时调用view()会触发不完整类型的错误。
方法1:添加finalize完成文档构建
修改查询builder的代码,添加finalize标记结束文档构建:
bsoncxx::builder::stream::document query_builder; query_builder << "username" << "Jake" << bsoncxx::builder::stream::finalize;
方法2:使用基础builder或初始化列表简化代码
无需使用stream builder,直接用基础builder构建查询文档:
auto cursor = myCollection.find( bsoncxx::builder::basic::make_document( bsoncxx::builder::basic::kvp("username", "Jake") ) );
或者用更简洁的C++初始化列表(驱动支持的情况下):
auto cursor = myCollection.find({{"username", "Jake"}});
优化include路径
现有include路径存在冗余,/opt/homebrew/include/mongocxx/v_noabi/和/opt/homebrew/include/bsoncxx/v_noabi/已包含所有子目录,可简化为:
"includePath": [ "${workspaceFolder}/**", "/opt/homebrew/include/mongocxx/v_noabi/", "/opt/homebrew/include/bsoncxx/v_noabi/", "/opt/homebrew/Cellar/mongo-cxx-driver/3.10.1/include/bsoncxx/v_noabi/bsoncxx/third_party/mnmlstc" ]
内容的提问来源于stack exchange,提问作者Dtal

