1. 程序安装
预安装
需要先安装依赖库OpenSSL,来建立ssl连接到MongoDB
RedHat / Fedora系统:
$ sudo yum install pkg-config openssl-devel cyrus-sasl-devel
Debian / Ubuntu系统:
$ sudo apt-get install pkg-config libssl-dev libsasl2-dev
FreeBSD系统:
$ su -c 'pkg install pkgconf openssl cyrus-sasl2'
参考:http://api.mongodb.org/c/1.3.3/installing.html#installing-unix
1.1. 安装MongoDB
安装MongoDB发行包
$ wget https://github.com/mongodb/mongo-c-driver/releases/download/1.3.3/mongo-c-driver-1.3.3.tar.gz
$ tar xzf mongo-c-driver-1.3.3.tar.gz
$ cd mongo-c-driver-1.3.3
$ . /configure --help
$ ./configure --enable-static --enable-shared
安装成功会出现如下配置描述
libmongoc was configured with the following options:
Build configuration:
Enable debugging (slow) : no
Compile with debug symbols (slow) : no
Enable GCC build optimization : yes
Enable automatic binary hardening : yes
Code coverage support : no
Cross Compiling : no
Fast counters : no
SASL : sasl2
SSL : yes
Libbson : bundled
Documentation:
Generate man pages : no
Install man pages : no
其中libbson是附带的捆绑的库,系统若无,脚本会自动安装。
$ make
$ sudo make install
find -name *.a 查询本目录下编译出来的静态库
1.2. 测试安装MongoDB
连接到MongoDB并测试
$ mongo --host localhost --port 27017
MongoDB shell version: 3.0.6
connecting to: localhost:27017/test
2. api操作
Libmongoc提供一系列的存储访问接口,以及存储和传输结构使用类型。其提供的为同步接口。
2.1. 创建连接
使用libmongoc驱动创建到mongo实例的连接
#include <bson.h>
#include <bcon.h>
#include <mongoc.h>
int
main (int argc,
char *argv[])
{
mongoc_client_t *client;
mongoc_database_t *database;
mongoc_collection_t *collection;
bson_t *command,
reply,
*insert;
bson_error_t error;
char *str;
bool retval;
/*
* Required to initialize libmongoc's internals
*/
mongoc_init ();//初始化libmongoc驱动
/*
* Create a new client instance
*/
client = mongoc_client_new ("mongodb://localhost:27017");//创建连接对象
/*
* Get a handle on the database "db_name" and collection "coll_name"
*/
database = mongoc_client_get_database (client, "db_name");//获取数据库
collection = mongoc_client_get_collection (client, "db_name", "coll_name");//获取指定数据库和集合
/*
* Do work. This example pings the database, prints the result as JSON and
* performs an insert
*/
command = BCON_NEW ("ping", BCON_INT32 (1));
retval = mongoc_client_command_simple (client, "admin", command, NULL, &reply, &error);//执行命令
if (!retval) {
fprintf (stderr, "%s\n", error.message);
return EXIT_FAILURE;
}
str = bson_as_json (&reply, NULL);
printf ("%s\n", str);
insert = BCON_NEW ("hello", BCON_UTF8 ("world"));//字段为hello,值为world字符串
if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, insert, NULL, &error)) {//插入文档
fprintf (stderr, "%s\n", error.message);
}
bson_destroy (insert);
bson_destroy (&reply);
bson_destroy (command);
bson_free (str);
/*
* Release our handles and clean up libmongoc
*/
mongoc_collection_destroy (collection);//释放表对象
mongoc_database_destroy (database);//释放数据库对象
mongoc_client_destroy (client);//释放连接对象
mongoc_cleanup ();//释放libmongoc驱动
return 0;
}
2.2. 文档
MongoDB使用了BSON这种结构来存储数据和网络数据交换。把这种格式转化成一文档这个概念,这里的一个文档也可以理解成关系数据库中的一条记录,只是这里的文档的变化更丰富一些,如文档可以嵌套。MongoDB以BSON做为其存储结构的一种重要原因是其可遍历性。
2.3. 创建文档
mongodb c驱动使用libbson来创建BSON文档。有几种构建文档的方式:使用BSON插入键值对,或者解析json。