自己收集了一些安装时候看的文章
https://www.jianshu.com/p/d77680254418
https://www.jianshu.com/p/c982a2960175
https://blog.51cto.com/juestnow/1950039
https://github.com/mongodb/mongo-cxx-driver
http://mongocxx.org/mongocxx-v3/installation/
http://mongoc.org/libmongoc/1.13.0/tutorial.html#basic-crud-operations
http://mongoc.org/libmongoc/current/installing.html
安装C++驱动之前要安装好C的驱动
安装CXX
git clone https://github.com/mongodb/mongo-cxx-driver.git
cd mongo-cxx-driver
cd build/
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig cmake -DCMAKE_BUILD_TYPE=Release -DBSONCXX_POLY_USE_MNMLSTC=1 -DCMAKE_INSTALL_PREFIX=/usr/local ..
sudo make EP_mnmlstc_core
sudo make && sudo make install
C的测试
往集合插入hello world
/*
gcc -o insert insert.c -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/libbson-1.0 -I/usr/local/include/libbson-1.0/bson/ -lmongoc-1.0 -lbson-1.0
*/
#include <bson/bson.h>
#include <mongoc/mongoc.h>
#include <stdio.h>
int
main (int argc,
char *argv[])
{
mongoc_client_t *client;
mongoc_collection_t *collection;
bson_error_t error;
bson_oid_t oid;
bson_t *doc;
mongoc_init ();
client = mongoc_client_new ("mongodb://localhost:27017/?appname=insert-example");
collection = mongoc_client_get_collection (client, "mydb", "mycoll");
doc = bson_new ();
bson_oid_init (&oid, NULL);
BSON_APPEND_OID (doc, "_id", &oid);
BSON_APPEND_UTF8 (doc, "hello", "world");
if (!mongoc_collection_insert_one (
collection, doc, NULL, NULL, &error)) {
fprintf (stderr, "%s\n", error.message);
}
bson_destroy (doc);
mongoc_collection_destroy (collection);
mongoc_client_destroy (client);
mongoc_cleanup ();
return 0;
}
C++的测试
/*
g++ --std=c++11 test.cpp -o test -I/usr/local/include/mongocxx/v_noabi \
-I/usr/local/include/bsoncxx/v_noabi \
-L/usr/local/lib -lmongocxx -lbsoncxx
*/
#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["mydb"]["mycoll"];
document << "hello" << "world";
collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
如果出现这个问题,需要把/usr/local/lib目录加在系统默认的库搜索目录中
$ ./test
./test: error while loading shared libraries: libmongocxx.so._noabi: cannot open shared object file: No such file or directory
sudo vi /etc/ld.so.conf
增加 /usr/local/lib
sudo /sbin/ldconfig -v;执行即可
至此,测试成功(python安装的话直接pip install pymongo完事了,cxx还要这么多事情)
本文详细介绍了MongoDB的C/C++驱动安装过程,包括安装C驱动、CXX驱动,以及解决可能出现的问题,如添加库搜索路径。通过一系列文章链接和官方文档,指导读者完成从安装到测试的基本 CRUD 操作。

721

被折叠的 条评论
为什么被折叠?



