一、编译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. struts2-(2)HelloWorld

    1.环境配置 1).进入http://struts.apache.org/download.cgi#struts23241 下载 struts官方源码 2).解压,进入apps/struts2-bla ...

  2. [BZOJ1056][BZOJ1862][HAOI2008][Zjoi2006]排名系统

    [BZOJ1056][BZOJ1862][HAOI2008][Zjoi2006]排名系统 试题描述 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录 ...

  3. [codeforces 360]A. Levko and Array Recovery

    [codeforces 360]A. Levko and Array Recovery 试题描述 Levko loves array a1, a2, ... , an, consisting of i ...

  4. hiho #1372:平方求 (bfs)

    #1372 : 平方求和 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 对于一个非负整数n,最少需要几个完全平方数,使其和为n? 输入 输入包含多组数据.对于每组数据: ...

  5. 2016年10月12日--string、Math类、Random随机数、DateTime、异常保护

    string string.length; //得到string长度 string.Trim(); //去掉string前后的空格 string.TrimStart(); //去掉string前的空格 ...

  6. HackerRank training-the-army

    Description 有 \(n\) 个技能,每次可以通过一个巫师,将一个技能转化成另一个技能,问最有最多有多少不同的技能. Sol 网络流. 先说说我一开始非常 naive 的建图,将技能拆点,中 ...

  7. 31 GroupSock(AddressString)——live555源码阅读(四)网络

    31 GroupSock(AddressString)——live555源码阅读(四)网络 31 GroupSock(AddressString)——live555源码阅读(四)网络 简介 Addre ...

  8. Ext 下拉列表模糊搜索

    /** * Created by huangbaidong on 2016/9/18. * 楼盘通用Combo组件,支持模糊查询 * 使用案例: * { fieldLabel : '楼盘名称', xt ...

  9. pycharm 中 pep8 检查开启.

    pycharm pep8检查的开启,默认是暗黄色,我这里为了醒目给改成了黄色.

  10. C++中using的作用

    [转自] http://www.cnblogs.com/yuaqua/archive/2011/10/22/2219991.html 1.在当前文件中引入命名空间 这是我们最熟悉的用法,例如:usin ...