|
最近用到MongoDB C驱动程序,网上资料较少,所以记录分享。
一、简介
MongoDB C驱动程序(也称为“ libmongoc”)是一个库,用于C程序中操作MongoDB。
官方下载地址:http://mongoc.org/
百度云分享:https://pan.baidu.com/s/1pqRje3zrh-mnEiMGr-ERlg 提取码:y5t1
libmongoc库:https://pan.baidu.com/s/16DF1qpxCTyp2oQmicjtiOw 提取码:z4yn
libbson库:https://pan.baidu.com/s/1di14LTcLTT_T2ta_e21lUg 提取码:f8o3
二、使用
1、安装并运行MongoDB
$ mongo --host localhost --port 27017
MongoDB shell version: 3.0.6
connecting to: localhost:27017/test
>
2、C程序中包含libmongoc
#include <mongoc/mongoc.h>
#include <bson/bson.h>
如果您不使用CMake或pkg-config,则可以手动管理路径和库。
$ gcc -o hello_mongoc hello_mongoc.c \
-I/usr/local/include/libbson-1.0 -I/usr/local/include/libmongoc-1.0 \
-lmongoc-1.0 -lbson-1.0
$ ./hello_mongoc
{ "ok" : 1.000000 }
3、建立连接
#include <mongoc/mongoc.h>
int main(int argc, char *argv[])
{
const char *uri_string = "mongodb://localhost:27017";
mongoc_uri_t *uri;
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 ();
/*
* Optionally get MongoDB URI from command line
*/
if (argc > 1) {
uri_string = argv[1];
}
/*
* Safely create a MongoDB URI object from the given string
*/
uri = mongoc_uri_new_with_error (uri_string, &error);
if (!uri) {
fprintf (stderr,
"failed to parse URI: %s\n"
"error message: %s\n",
uri_string,
error.message);
return EXIT_FAILURE;
}
/*
* Create a new client instance
*/
client = mongoc_client_new_from_uri (uri);
if (!client) {
return EXIT_FAILURE;
}
/*
* Register the application name so we can track it in the profile logs
* on the server. This can also be done from the URI (see other examples).
*/
mongoc_client_set_appname (client, "connect-example");
/*
* 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"));
if (!mongoc_collection_insert_one (collection, insert, NULL, 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_uri_destroy (uri);
mongoc_client_destroy (client);
mongoc_cleanup ();
return EXIT_SUCCESS;
}
三、常用接口
1、mongoc_collection_t * mongoc_client_new (const char *uri_string);
使用提供的URI字符串创建一个新的mongoc_client_t。
参数 uri_string:包含MongoDB连接URI的字符串。 返回 如果URI成功解析,则为新分配的mongoc_client_t,否则为NULL。
2、mongoc_collection_t * mongoc_client_get_collection (mongoc_client_t *client,const char *db,const char *collection);
参数
client:一个mongoc_client_t。
db:包含集合的数据库的名称。
collection:集合的名称。
返回
新分配的mongoc_collection_t,当不再使用时应使用mongoc_collection_destroy()释放它。
3、mongoc_cursor_t * mongoc_collection_find_with_opts (mongoc_collection_t * collection,
const bson_t * filter,
const bson_t * opts,
const mongoc_read_prefs_t* read_prefs)
参数 collection:一个mongoc_collection_t。 filter:bson_t包含要执行的查询的。 opts:bson_t查询选项,包括排序顺序和要返回的字段。可以NULL。 read_prefs:mongoc_read_prefs_t或NULL。
返回 新分配的mongoc_cursor_t,必须使用mongoc_cursor_destroy()释放。
来源:https://www.cnblogs.com/yinguojin/p/13251968.html |