ZBLOG

mongodb c的驱动封装接口,采用c++的写法,支持增删改查的功能

以下是一个MongoDB C++驱动的封装接口示例,支持增删改查的功能:

#include <iostream>
#include <mongoc.h>

class MongoDB {
public:
    MongoDB(const char* uri_str) {
        mongoc_init();
        this->client = mongoc_client_new(uri_str);
    }

    ~MongoDB() {
        mongoc_client_destroy(this->client);
        mongoc_cleanup();
    }

    bool insert(const char* database, const char* collection, const bson_t* document) {
        mongoc_collection_t* coll = mongoc_client_get_collection(this->client, database, collection);

        bson_error_t error;
        bool success = mongoc_collection_insert_one(coll, document, NULL, &error);

        if (!success) {
            std::cerr << "Failed to insert document: " << error.message << std::endl;
        }

        mongoc_collection_destroy(coll);

        return success;
    }

    bool update(const char* database, const char* collection, const bson_t* query, const bson_t* update) {
        mongoc_collection_t* coll = mongoc_client_get_collection(this->client, database, collection);

        bson_error_t error;
        bool success = mongoc_collection_update_one(coll, query, update, NULL, &error);

        if (!success) {
            std::cerr << "Failed to update document: " << error.message << std::endl;
        }

        mongoc_collection_destroy(coll);

        return success;
    }

    bool remove(const char* database, const char* collection, const bson_t* query) {
         mongoc_collection_t* coll = mongoc_client_get_collection(this->client, database,collection);

         bson_error_t error;
         bool success =  mongoc_collection_delete_one (coll ,query,NULL,&error );

         if(!success){
            std::cerr<<"Failed to remove document: "<<error.message<<std::endl;
         }

         mongoc_collection_destroy(coll);

         return success;
    }

    bson_t* find(const char* database, const char* collection, const bson_t* query) {
        mongoc_collection_t* coll = mongoc_client_get_collection(this->client, database, collection);

        mongoc_cursor_t* cursor = mongoc_collection_find_with_opts(coll, query, NULL, NULL);
        const bson_t *doc;

        while (mongoc_cursor_next(cursor, &doc)) {
            char *str = bson_as_json(doc, NULL);
            std::cout << str << std::endl;
            bson_free(str);
        }

        bson_error_t error;
        if (mongoc_cursor_error(cursor, &error)) {
            std::cerr << "Failed to read documents: " << error.message << std::endl;
        }

        mongoc_cursor_destroy(cursor);
        mongoc_collection_destroy(coll);

        return NULL; // 返回查询结果
    }

private:
    mongoc_client_t* client;
};

int main() {
    MongoDB mongo("mongodb://localhost:27017");

    // 插入数据
    bson_t *document = BCON_NEW("name", BCON_UTF8("John"), "age", BCON_INT32(25));
    bool result = mongo.insert("test", "users", document); // test为数据库名,users为集合名

    if (!result) {
        std::cerr << "Failed to insert document" << std::endl;
    }

    // 更新数据
    bson_t *query = BCON_NEW("name", BCON_UTF8("John"));
    bson_t *update = BCON_NEW("$set", "{", "age", BCON_INT32(30), "}");
    result = mongo.update("test", "users", query, update);

    if (!result) {
        std::cerr << "Failed to update document" << std::endl;
    }

    // 删除数据
    bson_t *remove_query = BCON_NEW("name", BCON_UTF8("John"));
    result = mongo.remove("test", "users", remove_query);

    if(!result){
        std::cerr<<"Failed to remove document"<<std::endl;
    }

    // 查询数据
    bson_t *find_query = BCON_NEW();
    mongo.find("test", "users", find_query);

    return 0;
}

这个示例中,我们创建了一个名为MongoDB的类,并在其中封装了插入、更新、删除和查询操作的函数。在主函数中,我们实例化MongoDB类,并分别执行插入、更新、删除和查询操作。

需要注意的是,在使用MongoDB C++驱动时,我们需要先调用mongoc_init()来初始化驱动库,在程序结束时还要调用mongoc_cleanup()进行清理。此外,所有与数据库交互的操作都需要通过获取集合对象(mongoc_collection_t)来完成。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=6082

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?