安装mongoDB-c-driver
安装必要软件
sudo apt-get install git autoconf automake libtool
下载mongoDB源码包
$ git clone https://github.com/mongodb/mongo-c-driver.git $ cd mongo-c-driver $ ./autogen.sh --with-libbson=bundled $ make $ sudo make install
将头文件以及库加入到系统路径中
export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/local/include/libmongoc-1.0/:/usr/local/include/libbson-1.0/
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/lib
使用mongoDB-c-driver编程
初始化mongoc
非线程安全,只需调用一次
mongoc_init();
连接mongodb
const char *uristr = "mongodb://127.0.0.1/"; mongoc_client_t* m_pClient = mongoc_client_new(uristr);
获取collection
mongoc_collection_t * m_pCollection = mongoc_client_get_collection(m_pClient, "test_db", "test_collection");
插入记录
bson_error_t error; bson_t *doc = bson_new(); BSON_APPEND_INT64(doc, "id", 1); BSON_APPEND_INT64(doc, "field1", 0); char msg[] = "test message"; BSON_APPEND_BINARY(doc, "field2", BSON_SUBTYPE_BINARY, msg, strlen(msg)); int r = mongoc_collection_insert(m_pCollection, MONGOC_INSERT_NONE, doc, NULL, &error); if (r == 0) { printf("Insert Failure:%s\n", error.message); } bson_destroy(doc);
完整代码:
int main() { const char *uristr = "mongodb://192.168.1.105/"; mongoc_init(); mongoc_client_t* m_pClient = mongoc_client_new(uristr); mongoc_collection_t * m_pCollection = mongoc_client_get_collection(m_pClient, "test_db", "test_collection"); bson_error_t error; bson_t *doc = bson_new(); BSON_APPEND_INT64(doc, "id", 1); BSON_APPEND_INT64(doc, "field1", 0); char msg[] = "test message"; BSON_APPEND_BINARY(doc, "field2", BSON_SUBTYPE_BINARY, msg, strlen(msg)); int r = mongoc_collection_insert(m_pCollection, MONGOC_INSERT_NONE, doc, NULL, &error); if (r == 0) { printf("Insert Failure:%s\n", error.message); } bson_destroy(doc); mongoc_client_destroy(m_pClient); return 0; }
更新记录
/*{$set:{field1:22}}*/ bson_error_t error; bson_t *doc = bson_new(); bson_t child; bson_append_document_begin(doc, "$set", -1, &child); BSON_APPEND_INT64(&child, "field1", 22); bson_append_document_end(doc, &child); /*{id:1}*/ bson_t query; bson_init(&query); BSON_APPEND_INT64(&query, "id", 1); int r = mongoc_collection_update(m_pCollection, MONGOC_UPDATE_NONE, &query, doc, NULL, &error); if (r == 0) { printf("Update Failure: %s\n", error.message); } bson_destroy(&query); bson_destroy(doc);
完整代码:
int main() { const char *uristr = "mongodb://192.168.64.10/"; mongoc_init(); mongoc_client_t* m_pClient = mongoc_client_new(uristr); mongoc_collection_t * m_pCollection = mongoc_client_get_collection(m_pClient, "test_db", "test_collection"); bson_error_t error; bson_t *doc = bson_new(); bson_t child; bson_append_document_begin(doc, "$set", -1, &child); BSON_APPEND_INT64(&child, "field1", 22); bson_append_document_end(doc, &child); bson_t query; bson_init(&query); BSON_APPEND_INT64(&query, "id", 1); int r = mongoc_collection_update(m_pCollection, MONGOC_UPDATE_NONE, &query, doc, NULL, &error); if (r == 0) { printf("Update Failure: %s\n", error.message); } bson_destroy(&query); bson_destroy(doc); mongoc_client_destroy(m_pClient); return 0; }
删除记录
bson_error_t error; bson_t query; bson_init(&query); BSON_APPEND_INT64(&query, "id", 1); int r = mongoc_collection_remove(m_pCollection, MONGOC_DELETE_NONE, &query, NULL, &error); if (r == 0) { printf("Delete Failure: %s\n", error.message); } bson_destroy(&query);
完整代码:
int main() { const char *uristr = "mongodb://192.168.1.105/"; mongoc_init(); mongoc_client_t* m_pClient = mongoc_client_new(uristr); mongoc_collection_t * m_pCollection = mongoc_client_get_collection(m_pClient, "test_db", "test_collection"); bson_error_t error; bson_t query; bson_init(&query); BSON_APPEND_INT64(&query, "id", 1); int r = mongoc_collection_remove(m_pCollection, MONGOC_DELETE_NONE, &query, NULL, &error); if (r == 0) { printf("Delete Failure: %s\n", error.message); } bson_destroy(&query); mongoc_client_destroy(m_pClient); return 0; }
编译命令
gcc mongoDBtest.c -lmongoc-1.0 -lbson-1.0
官方api
http://api.mongodb.com/
C++调用mongoDB
linux下安装mongoDB
下载mongo_2_2_3.tar.gz数据库的运行包
tar zxvf mongo22_3.tar.gz
mkdir ~/mongoDB
./bin/mongod --dbpath ~/mongoDB
运行成功如下图:
安装mongoDB C++驱动库
需要 libbson 和 MongoDB C driver预先安装配置妥当,而MongoDB C driver又要求automake
, autoconf
and libtool
,MongoDB C++ driver要求git
和pkg-config
sudo apt-get install git pkg-config cmake -y
安装完毕后查看cmake版本
cmake -version
安装mongDB C++驱动
下载地址https://github.com/mongodb/mongo-cxx-driver/releases,
tar -zxvf mongo-cxx-driver-r3.0.2.tar.gz cd mongo-cxx-driver-r3.0.2/build cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
注意第四步中使用选项 -DCMAKE_INSTALL_PREFIX=/usr/local 来指定将要安装C++驱动的路径如果不指定的话,将默认安装在 mongo-cxx-driver-r3.0.2/build/install 路径下将会导致找不到相应的头文件和库而无法编译
sudo make sudo make install
注意第二步一定要使用sudo来做,因为在上面生成makefile文件时指定了安装到 /usr/local/ 这个路径下该路径需要root权限才能写入
安装成功后,可以看到 /usr/local/lib 目录下已经成功的生成了相关的库文件,如下图所示
最后测试一下驱动,使用Mongodb官方给的测试代码,代码如下:
int main(int, char**)
{
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
bsoncxx::builder::stream::document document{};
auto collection = conn["testdb"]["testcollection"];
document << "hello" << "world";
collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto && doc : cursor)
{
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
使用下面的命令进行编译:
c++ --std=c++11 hellomongo.cpp -o hellomongo $(pkg-config --cflags --libs libmongocxx)