Linux C/C++调用mongDB

c调用mongoDB

安装mongoDB-c-driver

  1. 安装必要软件

sudo apt-get install git autoconf automake libtool
  1. 下载mongoDB源码包

$ git clone https://github.com/mongodb/mongo-c-driver.git
$ cd mongo-c-driver
$ ./autogen.sh --with-libbson=bundled
$ make
$ sudo make install
  1. 将头文件以及库加入到系统路径中

export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/local/include/libmongoc-1.0/:/usr/local/include/libbson-1.0/
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/lib

使用mongoDB-c-driver编程

初始化mongoc

非线程安全,只需调用一次

mongoc_init();

连接mongodb

const char *uristr = "mongodb://127.0.0.1/";
mongoc_client_t* m_pClient = mongoc_client_new(uristr);

获取collection

mongoc_collection_t * m_pCollection = mongoc_client_get_collection(m_pClient, "test_db", "test_collection"); 

插入记录

bson_error_t error;
bson_t *doc = bson_new();
BSON_APPEND_INT64(doc, "id", 1);
BSON_APPEND_INT64(doc, "field1", 0);
char msg[] = "test message";
BSON_APPEND_BINARY(doc, "field2", BSON_SUBTYPE_BINARY, msg, strlen(msg));

int r = mongoc_collection_insert(m_pCollection, MONGOC_INSERT_NONE, doc, NULL, &error);
if (r == 0)
{
    printf("Insert Failure:%s\n",  error.message);
}
bson_destroy(doc);

完整代码:


#include <stdio.h>
#include <mongoc.h>
#include <bson.h>
int main() {

    const char *uristr = "mongodb://192.168.1.105/";
    mongoc_init();
    mongoc_client_t* m_pClient = mongoc_client_new(uristr);
    mongoc_collection_t * m_pCollection = mongoc_client_get_collection(m_pClient, "test_db", "test_collection");

    bson_error_t error;
    bson_t *doc = bson_new();
    BSON_APPEND_INT64(doc, "id", 1);
    BSON_APPEND_INT64(doc, "field1", 0);
    char msg[] = "test message";
    BSON_APPEND_BINARY(doc, "field2", BSON_SUBTYPE_BINARY, msg, strlen(msg));

    int r = mongoc_collection_insert(m_pCollection, MONGOC_INSERT_NONE, doc, NULL, &error);
    if (r == 0)
    {
        printf("Insert Failure:%s\n",  error.message);
    }
    bson_destroy(doc);
    mongoc_client_destroy(m_pClient);
    return 0;
}

更新记录

/*{$set:{field1:22}}*/
bson_error_t error;
bson_t *doc = bson_new();
bson_t child;
bson_append_document_begin(doc, "$set", -1, &child);
BSON_APPEND_INT64(&child, "field1", 22);
bson_append_document_end(doc, &child);


/*{id:1}*/
bson_t query;
bson_init(&query);
BSON_APPEND_INT64(&query, "id", 1);

int r = mongoc_collection_update(m_pCollection, MONGOC_UPDATE_NONE,
                    &query, doc, NULL, &error);
if (r == 0)
{
    printf("Update Failure: %s\n", error.message);
}
bson_destroy(&query);
bson_destroy(doc);

完整代码:


#include <stdio.h>
#include <mongoc.h>
#include <bson.h>
int main() {

    const char *uristr = "mongodb://192.168.64.10/";
    mongoc_init();
    mongoc_client_t* m_pClient = mongoc_client_new(uristr);
    mongoc_collection_t * m_pCollection = mongoc_client_get_collection(m_pClient, "test_db", "test_collection");

    bson_error_t error;
    bson_t *doc = bson_new();
    bson_t child;
    bson_append_document_begin(doc, "$set", -1, &child);
    BSON_APPEND_INT64(&child, "field1", 22);
    bson_append_document_end(doc, &child);

    bson_t query;
    bson_init(&query);
    BSON_APPEND_INT64(&query, "id", 1);

    int r = mongoc_collection_update(m_pCollection, MONGOC_UPDATE_NONE,
                                    &query, doc, NULL, &error);
    if (r == 0)
    {
        printf("Update Failure: %s\n", error.message);
    }
    bson_destroy(&query);
    bson_destroy(doc);
    mongoc_client_destroy(m_pClient);

    return 0;
}

删除记录

bson_error_t error;
bson_t query;
bson_init(&query);
BSON_APPEND_INT64(&query"id"1);
int r = mongoc_collection_remove(m_pCollection, MONGOC_DELETE_NONE,
             &query, NULL,  &error);
if (r == 0)
{
    printf("Delete Failure: %s\n", error.message);
}
bson_destroy(&query);

完整代码:


#include <stdio.h>
#include <mongoc.h>
#include <bson.h>
int main() {

    const char *uristr = "mongodb://192.168.1.105/";
    mongoc_init();
    mongoc_client_t* m_pClient = mongoc_client_new(uristr);
    mongoc_collection_t * m_pCollection = mongoc_client_get_collection(m_pClient, "test_db", "test_collection");

    bson_error_t error;
    bson_t query;
    bson_init(&query);
    BSON_APPEND_INT64(&query, "id", 1);
    int r = mongoc_collection_remove(m_pCollection, MONGOC_DELETE_NONE,
                     &query, NULL,  &error);
    if (r == 0)
    {
            printf("Delete Failure: %s\n", error.message);
    }
    bson_destroy(&query);

    mongoc_client_destroy(m_pClient);
    return 0;
}

编译命令

gcc mongoDBtest.c -lmongoc-1.0 -lbson-1.0

官方api

http://api.mongodb.com/

C++调用mongoDB

linux下安装mongoDB

  • 下载mongo_2_2_3.tar.gz数据库的运行包

  • tar zxvf mongo22_3.tar.gz

  • mkdir ~/mongoDB

  • ./bin/mongod --dbpath ~/mongoDB

运行成功如下图:

安装mongoDB C++驱动库

​ 需要 libbson 和 MongoDB C driver预先安装配置妥当,而MongoDB C driver又要求automakeautoconf and libtoolMongoDB C++ driver要求gitpkg-config

sudo apt-get install git pkg-config cmake -y

安装完毕后查看cmake版本

cmake -version

安装mongDB C++驱动

下载地址https://github.com/mongodb/mongo-cxx-driver/releases

tar -zxvf mongo-cxx-driver-r3.0.2.tar.gz 
cd mongo-cxx-driver-r3.0.2/build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local

​ 注意第四步中使用选项 -DCMAKE_INSTALL_PREFIX=/usr/local 来指定将要安装C++驱动的路径如果不指定的话,将默认安装在 mongo-cxx-driver-r3.0.2/build/install 路径下将会导致找不到相应的头文件和库而无法编译

sudo make
sudo make install

​ 注意第二步一定要使用sudo来做,因为在上面生成makefile文件时指定了安装到 /usr/local/ 这个路径下该路径需要root权限才能写入

​ 安装成功后,可以看到 /usr/local/lib 目录下已经成功的生成了相关的库文件,如下图所示

​ 最后测试一下驱动,使用Mongodb官方给的测试代码,代码如下:

#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>

int main(int, char**)
{
    mongocxx::instance inst{};
    mongocxx::client conn{mongocxx::uri{}};

    bsoncxx::builder::stream::document document{};

    auto collection = conn["testdb"]["testcollection"];
    document << "hello" << "world";

    collection.insert_one(document.view());
    auto cursor = collection.find({});

    for (auto && doc : cursor)
    {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }
}

使用下面的命令进行编译: 

c++ --std=c++11 hellomongo.cpp -o hellomongo $(pkg-config --cflags --libs libmongocxx)

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值