libmongoc庫和libbson庫的使用
libmongoc庫和libbson庫的使用
學習一項知識,最好的方式是檢視官方說明書。
前言
工作和學習過程中,需要用到資料庫,這裡挑選了MongoDB 非關係型資料庫。使用資料庫,肯定離不開資料庫的 增、刪、改、查。
MongoDB自帶了客戶端程式:mongosh.exe
,基本可以滿足使用者所有的操作需要。但是進行程式二次開發,我們還是需要使用 MongoDB Client Library.
本文主要介紹 libmongoc庫 和 libbson庫的常用函式,以及使用過程中的注意事項
一、libmongoc庫和libbson庫的介紹
官網介紹:
A Cross Platform MongoDB Client Library for C The MongoDB C Driver, also known as “libmongoc”, is a library for using MongoDB from C applications, and for writing MongoDB drivers in higher-level languages. It depends on libbson to generate and parse BSON documents, the native data format of MongoDB.
MongoDB的資料儲存格式:bson ,那麼就需要libbson庫。
資料庫原始資料:
{"id":1001,"name":"ZhangSan","age":18,"sex":"male","Chinese":93.0,"Math":100.0,"English":80.0} {"id":1002,"name":"LiSi","age":18,"sex":"male","Chinese":100.0,"Math":60.0,"English":80.0} {"id":1003,"name":"WangWu","age":19,"sex":"male","Chinese":90.0,"Math":100.0,"English":90.0} {"id":1004,"name":"ZhaoLiu","age":19,"sex":"male","Chinese":95.0,"Math":70.0,"English":80.0} {"id":1005,"name":"XiaoLi","age":18,"sex":"female","Chinese":100.0,"Math":100.0,"English":80.0}
二、libmongoc庫的使用
資料庫的CRUD(即create、read、update、delete)操作
2-1 create(增操作)
以json格式傳入,在表中建立一條bson。
實現程式碼
#include <mongoc/mongoc.h> static int lib_collection_create_document(const char *uri_string, const char *database_name, const char *collection_name, const char *json) { mongoc_client_t *client = NULL; mongoc_collection_t *collection = NULL; bson_error_t error; bson_oid_t oid; bson_t *doc = NULL; bson_t *json_d = NULL; int result = 0; /* create mongoc client instace */ client = mongoc_client_new(uri_string); /* create collection instace */ collection = mongoc_client_get_collection(client, database_name, collection_name); /* create bson instace */ doc = bson_new(); /* init bson oid. * oid is primary key in collection */ bson_oid_init(&oid, NULL); /* bson oid append bson_t */ BSON_APPEND_OID(doc, "_id", &oid); json_d = bson_new_from_json((unsigned char *)json, -1, &error); if (!json_d) { fprintf(stderr, "%s\n", error.message); result = -1; } bson_concat(doc, json_d); bson_destroy(json_d); if (!mongoc_collection_insert_one(collection, doc, NULL, NULL, &error)) { fprintf(stderr, "%s\n", error.message); result = -2; } /* free bson_t memory */ bson_destroy(doc); /* free mongoc_collection_t */ mongoc_collection_destroy(collection); /* free mongoc_client_t */ mongoc_client_destroy(client); return result; } int main(int argc, char *argv[]) { mongoc_init(); const char *uri_string = "mongodb://10.8.198.55:27017"; const char *database = "event_database"; const char *collection = "student_coll"; // add a json to collection char json[500] = {"{\"id\":1006,\"name\":\"XiaoHong\",\"age\":24,\"sex\":\"female\",\"Chinese\":74.0,\"Math\":85.0,\"English\":81.0}"}; if (0 == lib_collection_create_document(uri_string, database, collection, json)) { printf("Create successful : %s\n", json); } mongoc_cleanup(); return 0; }
執行結果:
2-2 read(查操作)
在表中查詢符合條件的document,通過陣列返回
實現程式碼:
#include <mongoc/mongoc.h>
static int lib_collection_read_document(const char *uri_string, const char *database_name, const char *collection_name, const char *json_opts,
char doc_list_arr[][300], int *num_of_docs)
{
mongoc_client_t *client = NULL;
mongoc_collection_t *collection = NULL;
mongoc_cursor_t *cursor = NULL;
bson_error_t error;
const bson_t *doc = NULL;
bson_t *query = NULL;
bson_t *json_d = NULL;
char *str = NULL;
int result = 0;
client = mongoc_client_new(uri_string);
collection = mongoc_client_get_collection(client, database_name, collection_name);
query = bson_new();
json_d = bson_new_from_json((unsigned char *)json_opts, -1, &error);
if (!json_d)
{
fprintf(stderr, "%s\n", error.message);
result = -1;
}
bson_concat(query, json_d);
bson_destroy(json_d);
cursor = mongoc_collection_find_with_opts(collection, query, NULL, NULL);
while (mongoc_cursor_next(cursor, &doc))
{
//str = bson_as_canonical_extended_json(doc, NULL);
//str = bson_as_relaxed_extended_json(doc, NULL);
str = bson_as_json(doc, NULL);
sprintf(doc_list_arr[*num_of_docs], "%s", str);
(*num_of_docs)++;
bson_free(str);
}
bson_destroy(query);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
return result;
}
int main(int argc, char *argv[])
{
mongoc_init();
const char *uri_string = "mongodb://10.8.198.55:27017";
const char *database = "event_database";
const char *collection = "student_coll";
/* read jsons from collection with options */
char json_opts[300] = {"{\"age\":18,\"sex\":\"male\"}"};
char doc_list_arr[100][300] = {0};
int num_of_docs = 0;
lib_collection_read_document(uri_string, database, collection, json_opts, doc_list_arr, &num_of_docs);
for (int i = 0; i < num_of_docs; i++)
{
printf("%s\n", doc_list_arr[i]);
}
mongoc_cleanup();
return 0;
}
執行結果:
2-3 update(改操作)
在表中查詢指定的json,符合條件的json進行修改
注意:這裡修改值的型別為string
實現程式碼:
#include <mongoc/mongoc.h>
#include <bson/bson.h>
static int lib_collection_update_document(const char *uri_string, const char *database_name, const char *collection_name, const char *json_opts,
const char *key, const char *value_string)
{
mongoc_collection_t *collection = NULL;
mongoc_client_t *client = NULL;
bson_error_t error;
mongoc_cursor_t *cursor = NULL;
const bson_t *doc = NULL;
bson_t *update = NULL;
bson_t *query = NULL;
int result = 0;
client = mongoc_client_new(uri_string);
collection = mongoc_client_get_collection(client, database_name, collection_name);
update = BCON_NEW("$set",
"{",
key,
BCON_UTF8(value_string),
"}");
query = bson_new_from_json((unsigned char *)json_opts, -1, &error);
if (!query)
{
fprintf(stderr, "%s\n", error.message);
result = -1;
}
cursor = mongoc_collection_find_with_opts(collection, query, NULL, NULL);
while (mongoc_cursor_next(cursor, &doc))
{
if (!mongoc_collection_update_one(collection, doc, update, NULL, NULL, &error))
{
fprintf(stderr, "%s\n", error.message);
result = -2;
break;
}
}
bson_destroy(query);
bson_destroy(update);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
return result;
}
int main(int argc, char *argv[])
{
mongoc_init();
const char *uri_string = "mongodb://10.8.198.55:27017";
const char *database = "event_database";
const char *collection = "student_coll";
/* update documents from collection wiht options */
char json_opts[300] = { "{\"id\":1006}" };
const char *key = "name";
const char *value = "XiaoHe";
if (0 == lib_collection_update_document(uri_string, database, collection, json_opts, key, value))
{
printf("Update successful\n");
}
mongoc_cleanup();
return 0;
}
執行結果:
2-4 delete(刪操作)
在表中查詢指定的json,符合條件的json進行刪除
實現程式碼:
#include <mongoc/mongoc.h>
#include <bson/bson.h>
static int lib_collection_delete_document(const char *uri_string, const char *database_name, const char *collection_name, const char *json_opts)
{
mongoc_client_t *client = NULL;
mongoc_collection_t *collection = NULL;
mongoc_cursor_t *cursor = NULL;
bson_error_t error;
const bson_t *doc = NULL;
bson_t *query = NULL;
int result = 0;
client = mongoc_client_new(uri_string);
collection = mongoc_client_get_collection(client, database_name, collection_name);
query = bson_new_from_json((unsigned char *)json_opts, -1, &error);
if (!query)
{
fprintf(stderr, "%s\n", error.message);
result = -1;
}
cursor = mongoc_collection_find_with_opts(collection, query, NULL, NULL);
while (mongoc_cursor_next(cursor, &doc))
{
if (!mongoc_collection_delete_one(collection, doc, NULL, NULL, &error))
{
fprintf(stderr, "%s\n", error.message);
result = -2;
}
}
bson_destroy(query);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
return result;
}
int main(int argc, char *argv[])
{
mongoc_init();
const char *uri_string = "mongodb://10.8.198.55:27017";
const char *database = "event_database";
const char *collection = "student_coll";
/* delete documents with options in collection */
char json_opts[300] = { "{\"age\":19}" };
if (0 == lib_collection_delete_document(uri_string, database, collection, json_opts))
{
printf("Delete successful\n");
}
mongoc_cleanup();
return 0;
}
執行結果:
三、libbson庫的使用
-
將bson格式轉換成json格式(value不帶型別)
函式:char * bson_as_json (const bson_t *bson, size_t *length);
輸出格式:
注意事項:函式返回值(指標)記得使用函式
void bson_free (void *mem)
釋放 -
將bson格式轉換成json格式(value帶型別)
函式:char * bson_as_canonical_extended_json (const bson_t *bson, size_t *length)
輸出格式:
注意事項:函式返回值(指標)記得使用函式
void bson_free (void *mem)
釋放