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
- #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;
- }
# 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