Usando CXX MongoDB Driver no MacOS
Para instalar o driver de conexão do MongoDb no MacOS tem que realizar os seguintes passos:
1 | brew install mongo-cxx-driver |
Cria o arquivo de teste test.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> #include <bsoncxx/builder/stream/document.hpp> #include <bsoncxx/json.hpp> #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> int main(int, char**) { mongocxx::instance inst{}; mongocxx::client conn{mongocxx::uri{}}; bsoncxx::builder::stream::document document{}; auto collection = conn["testedb"]["testecollection"]; document << "Mensagem" << "hello world"; collection.insert_one(document.view()); auto cursor = collection.find({}); for (auto&& doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; } } |
Compila o programa
1 2 3 | export LD_LIBRARY_PATH=/usr/local/lib/ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx) |
Executa o …