一、编译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. W3Cschool菜鸟教程离线版下载链接

    请在电脑上打开以下链接进行下载w3cschool 离线版(chm):http://pan.baidu.com/s/1bniwRCV(最新,2014年10月21日更新)w3cschool 离线版(htm ...

  2. jQuery对话框插件 ThickBox

    http://baike.haosou.com/doc/7607201-7881296.html 项目已经停止维护,但该插件还是不错的! ThickBox是一个基于JQuery类库的扩展,它能在浏览器 ...

  3. 备份Oracle数据库的脚本

    @echo off goto bakoracle :bakoracle echo. echo ★☆★  自动备份Oracle数据库   ★☆★ echo. set backpath=E:\Oracle ...

  4. 开源多线程性能测试工具-sysbench

    导读 sysbench是一款开源的多线程性能测试工具,可以执行CPU/内存/线程/IO/数据库等方面的性能测试.数据库目前支持MySQL/Oracle/PostgreSQL.本文主要演示Mysql测试 ...

  5. JDIC 访问Web时NullPointerException

    Exception in thread "EventThread" java.lang.NullPointerException            at org.jdeskto ...

  6. 子类重载父类的方法“parent::方法名”转于 恩聪PHP学习教程

    在PHP中不能定义重名的函数,也包括不能再同一个类中定义重名的方法,所以也就没有方法重载.单在子类中可以定义和父类重名的方法,因为父类的方法已经在子类中存在,这样在子类中就可以把从父类中继承过来的方法 ...

  7. python——tuple元组

    >>> dir(tuple) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', ...

  8. 图解SSL/TLS协议(HTTPS的安全层)

    http://blog.csdn.net/wallezhe/article/details/50977337 图解SSL/TLS协议     作者: 阮一峰 日期: 2014年9月20日 本周,Clo ...

  9. SQL Server日期和时间的格式

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  10. android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出的解决办法

    android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出的解决办法 原方法: public static Bitmap getSmallBitmap(Strin ...