一、编译mongodb c driver:


编译完成之后在c:\mongo-c-driver目录下有bin、include、lib三个文件夹,分别包含所需的dll、.h文件、lib。

在自己的项目中引入这些.h文件、lib、dll就可以使用 C API 了。

二、C API 的使用

1.连接MongoDB:

//只能调用一次mongoc_init
mongoc_init();

const char *uristr = "mongodb://127.0.0.1:27017/";//地址可配

//connection to MongoDB.
m_client = mongoc_client_new (uristr);

if (!m_client)
{
return FALSE;
}

m_pCollection = mongoc_client_get_collection(m_client, "myDatabase", "mycollection");
return TRUE;

2.Insert操作:

bson_t query;

bson_init(&query);
BSON_APPEND_UTF8(&query,"name","chunxiao");
BSON_APPEND_INT32(&query,"age",NULL);
BSON_APPEND_DOUBLE(&query,"Price",66.0);
if (!mongoc_collection_insert(m_pCollection,MONGOC_INSERT_NONE,&query,NULL,&error))
{
AfxMessageBox(error.message);
}
bson_destroy(&query);
return TRUE;

3.Find操作:

double age1 = 20;
double age2 = 22;
char name[10] = "chunxiao";
int sex = 0;

bson_init(&query);
bson_append_document_begin(&query,"age",3,&child);
bson_append_double(&child,"$gt",3,age1);
bson_append_document_end(&query,&child);

bson_append_document_begin(&query,"age",3,&child1);
bson_append_double(&child1,"$lte",4,age2);
bson_append_document_end(&query,&child1);

cursor = mongoc_collection_find(m_pCollection,MONGOC_QUERY_NONE,0,0,0,&query,NULL,NULL);
while (mongoc_cursor_more(cursor) && mongoc_cursor_next(cursor,&docFind))
{
bson_iter_t iter;
// bson_iter_init(&iter,docFind);
// if (bson_iter_find(&iter,"age"))
// {
// int f = bson_iter_int32(&iter);
// sprintf(str,"%d",f);
// AfxMessageBox(str);
// }
bson_iter_init(&iter,docFind);//顺序不一致的话,要重新初始化一下 iter,否则find不到
if (bson_iter_find(&iter,"name"))
{
strcpy(str,bson_iter_utf8(&iter,NULL));
strtest = bson_iter_utf8(&iter,NULL);
if (strcmp(strtest.c_str(),"xiaochun")==0)
{
AfxMessageBox(str);
continue;
}
strtest = strtest.substr(strtest.find_first_of("x"),2);
AfxMessageBox(str);
}
if (bson_iter_find(&iter,"age"))
{
int f = bson_iter_int32(&iter);
sprintf(str,"%d",f);
AfxMessageBox(str);
}
}
bson_destroy(&child);
bson_destroy(&child1);
bson_destroy(&query);
mongoc_cursor_destroy(cursor);

return TRUE;

4.Delete操作:

double age = 21;
bson_init(&query);
bson_append_document_begin(&query,"age",3,&child);
bson_append_double(&child,"$gt",3,age);
bson_append_document_end(&query,&child);
BSON_APPEND_UTF8(&query,"name","chun.xiao");
if (!mongoc_collection_remove(m_pCollection,MONGOC_REMOVE_NONE,&query,NULL,&error))
{
AfxMessageBox(error.message);
}
bson_destroy(&query);
return TRUE;

5.UpDate操作:

bson_init(&query);
BSON_APPEND_UTF8(&query,"name","chunxiao");

bson_t *docUpDate = bson_new();

bson_append_document_begin(docUpDate,"$set",-1,&child);
BSON_APPEND_UTF8(&child,"name","xiaochun");
bson_append_document_end(docUpDate,&child);

bson_init(&child);
bson_append_document_begin(docUpDate,"$set",-1,&child);
BSON_APPEND_UTF8(&child,"age","21");
bson_append_document_end(docUpDate,&child);

if (!mongoc_collection_update(m_pCollection,MONGOC_UPDATE_NONE,&query,docUpDate,NULL,&error))
{
AfxMessageBox(error.message);
}
bson_destroy(&query);
bson_destroy(docUpDate);
return TRUE;

百度云(13207134391):

  编译 C driver:

    MongoDB\C API\安装MongoDB以及编译 C driver

  Demo:

    MongoDB\C API\C API 操作MongoDB

 

MongoDB C API的更多相关文章

  1. mongodb c api编译

    1. autoconf-latest.tar.gz http://ftp.gnu.org/gnu/autoconf/ tar xzvf autoconf-latest.tar.gz ./configu ...

  2. springboot 学习之路 14(整合mongodb的Api操作)

    springboot整合mongodb: mongodb的安装和权限配置  请点击连接参考 mongodb集成 : 第一步:引如pom文件 第二步:配置文件配置mongodb路径: 第三步:关于mon ...

  3. MongoDB Java API操作很全的整理

    MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写,一般生产上建议以共享分片的形式来部署. 但是MongoDB官方也提供了其它语言的客户端操作API.如下图所示: 提供了C.C++ ...

  4. nodejs+express+mongodb写api接口的简单尝试

    1:启动mongodb服务 我的mongoDB的安装目录:E:\mongoDB\bin,版本:3.4.9 打开cmd  -> e:(进入e盘) -> cd mongoDB/bin(进入mo ...

  5. MongoDB JAVA API Filters

    Filters 该过滤器类为所有的MongoDB的查询操作静态工厂方法.每个方法返回BSON类型,又可以传递给期望一个查询过滤器的任何方法的一个实例. eq:匹配等于指定值的值.gt:匹配大于指定值的 ...

  6. MongoDB(3)--Java 操作 MongoDB 的 API

    引入mongoDb 依赖 <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-j ...

  7. Mongodb JAVA API

    连接mongodb 1.连接一个mongodb ); 2.连接mongodb集群 MongoClient mongoClient = ), new ServerAddress("localh ...

  8. MongoDB 基础API使用

    1. 基本概念: 1.1. MongoDB 保留数据库名: admin: 从权限的角度来看,这是"root"数据库.要是将一个用户添加到这个数据库,这个用户自动继承所有数据库的权限 ...

  9. gin mongodb restful api设计: 动态的patch接口

    目录 什么是Patch? Patch方法可以用来更新资源的一个组成部分 什么时候使用Patch? 当你仅需更新资源的某一项,即不完全也不幂等 那当我们的模型在数据库中几乎每个字段都可能会遇到改变的时候 ...

随机推荐

  1. Android Fragment完全解析,关于碎片你所需知道的一切

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...

  2. hiho一下 第一百零七周 Give My Text Back(微软笔试题)

    题目1 : Give My Text Back 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 To prepare for the English exam Littl ...

  3. OpenCV人形检测Hog

    #include "iostream" #include "queue" using namespace std; #include "opencv2 ...

  4. huffman编码压缩算法(转)

    参考:http://blog.csdn.net/sunmenggmail/article/details/7598012 笔试时遇到的一道题.

  5. 顺序栈的c++实现及利用其实现括号的匹配

    #include<iostream>#include<cassert>#include<cstring>#include<string>using na ...

  6. py代码模糊工具

    https://github.com/astrand/pyobfuscate 使用方法,调用下图文件即可,方法 python  pyobfuscate.py your_code.py 即可输出在cmd ...

  7. jQuery Plugin Docs

    遮罩:BlockUI 对话框:Dialog 输入框:Mask Plugin 下拉框: 1.自定义的实现思路:将下拉框的选项定义为全局变量(数组),在html中定义点击事件并将数组变量转给响应函数,每次 ...

  8. javascript中apply、call和bind的区别,容量理解,值得转!

    a)  javascript中apply.call和bind的区别:http://www.cnblogs.com/cosiray/p/4512969.html b)  深入浅出 妙用Javascrip ...

  9. appium + python 环境搭建

    所需:JDK.Android SDK.Appium服务程序.Appium客户端程序 1. 安装最新的JDK,并配置环境变量. JAVA_HOME=C:\Program Files (x86)\Java ...

  10. <转>VPN技术原理

    原文地址:VPN技术原理 VPN,Virtual Private Network(虚拟专用 网络),被定义为通过一个公用网络(通常是因特网)建立一个临时的.安全的连接,是一条穿过公用网络的安全.稳定的 ...