I am trying using MONGO DB C++ driver, and I am trying search my database if there is a field that equals a specific string.
The following code below works (implying I have mongoDB C++ drivers for installed correctly
void viewMongoDBServer()
{
cout <<"Check Mongo Connection" << endl;
// Establish an instance of mongocxx
mongocxx::instance instance{};
// Connect to MongoDB server
mongocxx::uri uri("mongodb://localhost:27017");
mongocxx::client client(uri);
// Access database UserData
mongocxx::database db = client["UserData"];
// Access the specific collection
mongocxx::collection collection = db["Users"];
// Query the collection and iterate over the documents
auto cursor = collection.find({});
for (auto&& doc : cursor) {
// Print each document
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
this code correctly print the following:
{
“_id”: {
“$oid”: “665d09b70296028f85120934”
},
“username”: “Jake”,
“password”: “Hello”
}
I have another function and all I want to do is search the documents inside of collection “Users” that equal specific username.
void searchDataInMongoDBServer()
{
cout << "Search MongoDB Connection" << endl;
// establish instance of mongocxx
mongocxx::instance instance{};
// conect to mongo DB server as a client
mongocxx::uri uri("mongodb://localhost:27017");
mongocxx::client client(uri);
// Lets access a specific data base
mongocxx::database db = client["UserData"];
// Now that we have access to the data base lets access a specific collectin
mongocxx::collection myCollection = db["Users"];
// we now have access to the collection insdie of our database
// lets search for documents in the collection
bsoncxx::builder::stream::document query_builder;
query_builder << "username" << "Jake";
// Execute the query
auto cursor = myCollection.find(query_builder.view());
// Iterate over the results
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
The error I receive:
/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)> {};
thes are libraries I have include in my main.cpp
#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>
below is the c_cpp_properties include path
"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"
Trying to build a query to search my mongoDB collection. but getting error: incomplete type