MongoDB-C驱动的安装

MAC环境下

一、安装openssl

$ brew install openssl
$ export LDFLAGS="-L/usr/local/opt/openssl/lib"
$ export CPPFLAGS="-I/usr/local/opt/openssl/include"

二、下载并解压安装包

$ curl -LO https://github.com/mongodb/mongo-c-driver/releases/download/1.4.0/mongo-c-driver-1.4.0.tar.gz
$ tar xzf mongo-c-driver-1.4.0.tar.gz
$ cd mongo-c-driver-1.4.0
三、编译并安装

$ ./configure
$ make
$ sudo make install
$ brew install yelp-xsl yelp-tools
$ ./configure --enable-html-docs --enable-man-pages
$ make man html
$ brew install automake autoconf libtool pkgconfig
四、连接代码 test.c

  1. #include <bson.h>  
  2. #include <bcon.h>  
  3. #include <mongoc.h>  
  4. int  
  5. main (int   argc,  
  6.       char *argv[])  
  7. {  
  8.    mongoc_client_t      *client;  
  9.    mongoc_database_t    *database;  
  10.    mongoc_collection_t  *collection;  
  11.    bson_t               *command,  
  12.                          reply,  
  13.                         *insert;  
  14.    bson_error_t          error;  
  15.    char                 *str;  
  16.    bool                  retval;  
  17.    /* 
  18.     * Required to initialize libmongoc's internals 
  19.     */  
  20.    mongoc_init ();//初始化libmongoc驱动  
  21.    /* 
  22.     * Create a new client instance 
  23.     */  
  24.    client = mongoc_client_new ("mongodb://localhost:27017");//创建连接对象  
  25.    /* 
  26.     * Get a handle on the database "db_name" and collection "coll_name" 
  27.     */  
  28.    database = mongoc_client_get_database (client, "db_name");//获取数据库  
  29.    collection = mongoc_client_get_collection (client, "db_name""coll_name");//获取指定数据库和集合  
  30.    /* 
  31.     * Do work. This example pings the database, prints the result as JSON and 
  32.     * performs an insert 
  33.     */  
  34.    command = BCON_NEW ("ping", BCON_INT32 (1));  
  35.    retval = mongoc_client_command_simple (client, "admin", command, NULL, &reply, &error);//执行命令  
  36.    if (!retval) {  
  37.       fprintf (stderr, "%s\n", error.message);  
  38.       return EXIT_FAILURE;  
  39.    }  
  40.    str = bson_as_json (&reply, NULL);  
  41.    printf ("%s\n", str);  
  42.    insert = BCON_NEW ("hello", BCON_UTF8 ("world"));//字段为hello,值为world字符串  
  43.    if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, insert, NULL, &error)) {//插入文档  
  44.       fprintf (stderr, "%s\n", error.message);  
  45.    }  
  46.    bson_destroy (insert);  
  47.    bson_destroy (&reply);  
  48.    bson_destroy (command);  
  49.    bson_free (str);  
  50.    /* 
  51.     * Release our handles and clean up libmongoc 
  52.     */  
  53.    mongoc_collection_destroy (collection);//释放表对象  
  54.    mongoc_database_destroy (database);//释放数据库对象  
  55.    mongoc_client_destroy (client);//释放连接对象  
  56.    mongoc_cleanup ();//释放libmongoc驱动  
  57.    return 0;  
五、编译执行

# gcc -o test test.c -I /usr/local/include/libmongoc-1.0 -I /usr/local/include/libbson-1.0/ -L /usr/local/lib/ -lmongoc-1.0 -lbson-1.0

# ./test 

{ "ok" : 1 }


Linux环境下

一、安装依赖包

$ apt-get install libmongoc-1.0-0
$ sudo apt-get install pkg-config libssl-dev libsasl2-dev
二、下载并解压安装驱动包

$ wget https://github.com/mongodb/mongo-c-driver/releases/download/1.4.0/mongo-c-driver-1.4.0.tar.gz
$ tar xzf mongo-c-driver-1.4.0.tar.gz
$ cd mongo-c-driver-1.4.0
$ ./configure
$ make
$ sudo make install

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值