一、编译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