通过C语言访问MongoDB
摘要
我使用基于MongoDB公式出现的C语言驱动程序创建了一个示例,用来访问MongoDB。我打算分享源代码以及操作步骤等内容。
搭建环境
我們以CentOS為基準。
添加Mongo的存储库。创建名为mongodb.repo的新文件。
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
yum update -y
yum install -y mongodb-org # mongodbインストール
# mongodbドライバインストール
yum install -y make git gcc automake autoconf libtool pkg-config openssl-devel cyrus-sasl-devel wget tar # driverインストールに必要なもの
wget https://github.com/mongodb/mongo-c-driver/releases/download/1.9.5/mongo-c-driver-1.9.5.tar.gz
tar xzf mongo-c-driver-1.9.5.tar.gz
cd mongo-c-driver-1.9.5
./configure
sudo make
sudo make install
数据库配置
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
yum update -y
yum install -y mongodb-org # mongodbインストール
# mongodbドライバインストール
yum install -y make git gcc automake autoconf libtool pkg-config openssl-devel cyrus-sasl-devel wget tar # driverインストールに必要なもの
wget https://github.com/mongodb/mongo-c-driver/releases/download/1.9.5/mongo-c-driver-1.9.5.tar.gz
tar xzf mongo-c-driver-1.9.5.tar.gz
cd mongo-c-driver-1.9.5
./configure
sudo make
sudo make install
我会输入测试数据。
service mongod start # mongodbサーバ起動
mongo # mongodbサーバ接続
use db_test # DB作成
# コレクション作成、ドキュメント投入
db.cl_test.insert({id:1, name:'aaa'});
db.cl_test.insert({id:2, name:'bbb'});
db.cl_test.insert({id:3, name:'ccc'});
exit
访问MongoDB
#include <bson.h>
#include <bcon.h>
#include <mongoc.h>
#include <stdio.h>
#include <stdlib.h>
int main( void ){
mongoc_client_t * conn = NULL;
mongoc_collection_t * coll = NULL;
mongoc_cursor_t * cusr = NULL;
const bson_t * doc = NULL;
char * ret_str = NULL;
bson_t query;
// DB接続
conn = mongoc_client_new (
"mongodb://localhost:27017/" // 接続先URI
);
if( NULL == conn ){
// error
exit(-1);
}
// データ取得
coll = mongoc_client_get_collection (
conn , // コネクション
"db_test" , // DB名
"cl_test" // コレクション名
);
if( NULL == coll ){
// error
mongoc_client_destroy ( conn );
exit(-1);
}
bson_init (&query);
cusr = mongoc_collection_find (
coll , // コレクション
MONGOC_QUERY_NONE , // mongoc_query_flags_t(検索条件なしを指定)
0 , // 開始位置(最初から取得)
0 , // 最大取得数(全て取得)
0 , // batchサイズ( default指定(=100) )
&query , // クエリ(指定なし)
NULL , // フィールド(全てを対象)
NULL // 取得クラスタ(default指定(=プライマリから取得))
);
if( NULL == cusr ){
// error
mongoc_client_destroy ( conn );
bson_destroy (&query);
exit(-1);
}
while (mongoc_cursor_next ( cusr , &doc ) ) {
ret_str = bson_as_json ( doc , NULL );
printf ( "%s\n", ret_str );
bson_free ( ret_str );
}
// 後片づけ
mongoc_collection_destroy( coll );
mongoc_client_destroy ( conn );
bson_destroy (&query);
return 0;
}
编译、执行
#include <bson.h>
#include <bcon.h>
#include <mongoc.h>
#include <stdio.h>
#include <stdlib.h>
int main( void ){
mongoc_client_t * conn = NULL;
mongoc_collection_t * coll = NULL;
mongoc_cursor_t * cusr = NULL;
const bson_t * doc = NULL;
char * ret_str = NULL;
bson_t query;
// DB接続
conn = mongoc_client_new (
"mongodb://localhost:27017/" // 接続先URI
);
if( NULL == conn ){
// error
exit(-1);
}
// データ取得
coll = mongoc_client_get_collection (
conn , // コネクション
"db_test" , // DB名
"cl_test" // コレクション名
);
if( NULL == coll ){
// error
mongoc_client_destroy ( conn );
exit(-1);
}
bson_init (&query);
cusr = mongoc_collection_find (
coll , // コレクション
MONGOC_QUERY_NONE , // mongoc_query_flags_t(検索条件なしを指定)
0 , // 開始位置(最初から取得)
0 , // 最大取得数(全て取得)
0 , // batchサイズ( default指定(=100) )
&query , // クエリ(指定なし)
NULL , // フィールド(全てを対象)
NULL // 取得クラスタ(default指定(=プライマリから取得))
);
if( NULL == cusr ){
// error
mongoc_client_destroy ( conn );
bson_destroy (&query);
exit(-1);
}
while (mongoc_cursor_next ( cusr , &doc ) ) {
ret_str = bson_as_json ( doc , NULL );
printf ( "%s\n", ret_str );
bson_free ( ret_str );
}
// 後片づけ
mongoc_collection_destroy( coll );
mongoc_client_destroy ( conn );
bson_destroy (&query);
return 0;
}
我们无法直接找到库,所以需要创建并注册mongo.conf后进行执行。
/usr/local/lib
ldconfig # 設定反映
gcc -Wall -o mongo mongo.c -lmongoc-1.0 -lbson-1.0 -L/usr/local/lib -I/usr/local/include/libmongoc-1.0/ -I/usr/local/include/libbson-1.0/
./mongo
{ "_id" : { "$oid" : "54bfc04676613f50e453d617" }, "id" : 1.000000, "name" : "aaa" }
{ "_id" : { "$oid" : "54bfc04676613f50e453d618" }, "id" : 2.000000, "name" : "bbb" }
{ "_id" : { "$oid" : "54bfc04976613f50e453d619" }, "id" : 3.000000, "name" : "ccc" }
解析C中的JSON似乎很麻烦。