资源简介
MongoDB C/C++开发使用案例Demo(含源代码例子、编译好的MongoDB C库文件(.h .lib .dll)、开发环境搭建配置文档等),可直接用于项目开发
代码片段和文件信息
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include “stdafx.h“
#include
#include
#include
#include
#include
#include
#include
using namespace std;
/**********************************************************************/
void UnicodeToUTF8(char *utf wchar_t* wszString)
{
//预转换,得到所需空间的大小
int u8Len = ::WideCharToMultiByte(CP_UTF8 NULL wszString wcslen(wszString) NULL 0 NULL NULL);
//同上,分配空间要给‘\0‘留个空间
//UTF8虽然是Unicode的压缩形式,但也是多字节字符串,所以可以以char的形式保存
//转换 //unicode版对应的strlen是wcslen
::WideCharToMultiByte(CP_UTF8 NULL wszString wcslen(wszString) utf u8Len NULL NULL);
//最后加上‘\0‘
utf[u8Len] = ‘\0‘;
}
#if 1
int _tmain(int argc _TCHAR* argv[])
{
mongoc_collection_t *collection;
bson_error_t error;
bson_t *doc; /*文档名称doc*/
const bson_t *doc_qurey;
bson_t child;
const char *uristr = “mongodb://localhost:27017“; /*mongodb地址*/
const char *collection_name = “col“; /*集合名称test*/
time_t now = time(0);
char timebuf[256] = {};
struct tm cur_time;
char *str;
//bson_t *query;
mongoc_cursor_t *cursor;
bson_t query;
bson_t child2 child3;
localtime_s(&cur_time &now);/*获取本地当前时间*/
strftime(timebuf sizeof(timebuf) “%Y-%m-%d %H:%M:%S“ &cur_time);/*格式化时间*/
const char *optime = timebuf;
//先调用mongoc_init(),libmongoc 的任何其他功能才能正确使用
mongoc_init();/*初始化*/
// Create a new client instance
mongoc_client_t *client = mongoc_client_new(uristr); /*连线到db */
if (NULL == client) {
fprintf(stderr “Failed to parse URI.\n“);
return EXIT_FAILURE;
}
mongoc_client_set_error_api(client 2);
//获取集合名
collection = mongoc_client_get_collection(client “test“ collection_name);/*获取集合*/
/******************写入操作例子如下:************************/
doc = bson_new();
BSON_APPEND_UTF8(doc “Titile“ “Test123 “);
BSON_APPEND_UTF8(doc “Nowtime“ optime);
//开始一个子文档
BSON_APPEND_DOCUMENT_BEGIN(doc “subdoc“ &child);
BSON_APPEND_UTF8(&child “subkey“ “value“);
bson_append_document_end(doc &child);
double dval = 12.34;
char *tm_sample = “2018:30“;
char buf_json[64] = { 0 };
sprintf_s(buf_json “[{\“sample_time\“:\“%s\“\“sample_value\“:\“%.2f\“}]“ tm_sample dval);
bson_t *bt_arrayOfSample = bson_new_from_json((const uint8_t *)buf_json
strlen(buf_json) &error);
BSON_APPEND_ARRAY(doc “arrayOfSample“ bt_arrayOfSample);
size_t err_offset;
if (!bson_validate(doc BSON_VALIDATE_NONE &err_offset)) {
fprintf(stderr
“The document failed to validate at offset: %u\n“
(unsigned)err_offset);
}
/*将bson文档插入到集合*/
if (!mongoc_collection_insert(collection MONGOC_INSERT_NONE doc NULL &error))
{
fprintf(stderr “%s\n“ error.message);
}
/**************************************批量写入操作例子如下:*************************************/
enum N { ndocs = 4 };
bson_t *docs_bulk[ndocs];
mongoc_bulk
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-03-19 17:20 MongoDB_Smaples\
目录 0 2018-03-19 16:24 MongoDB_Smaples\mongo-c-driver\
目录 0 2018-03-19 16:24 MongoDB_Smaples\mongo-c-driver\include\
目录 0 2018-03-19 16:24 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\
文件 10939 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bcon.h
文件 2836 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-atomic.h
文件 1007 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-clock.h
文件 3741 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-compat.h
文件 2770 2018-02-28 11:14 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-config.h
文件 1101 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-context.h
文件 1662 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-decimal128.h
文件 6474 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-endian.h
文件 1147 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-error.h
文件 12473 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-iter.h
文件 2216 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-json.h
文件 928 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-keys.h
文件 7327 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-macros.h
文件 2837 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-md5.h
文件 1687 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-memory.h
文件 6107 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-oid.h
文件 3431 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-reader.h
文件 7857 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-stdint-win32.h
文件 21 2018-02-28 11:14 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-stdint.h
文件 2461 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-string.h
文件 17705 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-types.h
文件 1276 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-utf8.h
文件 898 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-value.h
文件 1175 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-version-functions.h
文件 2500 2018-02-28 11:14 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-version.h
文件 1751 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson-writer.h
文件 33300 2018-02-26 12:02 MongoDB_Smaples\mongo-c-driver\include\libbson-1.0\bson.h
............此处省略67个文件信息
- 上一篇:poco-1.8.0.1
- 下一篇:centos7 oracle11g 缺少的包
相关资源
- centos7 oracle11g 缺少的包
- poco-1.8.0.1
- C++的pb_ds库在OI中的应用
- Visual C++ 2015 Build Tools264358
- 基于SDL的贪吃蛇游戏
- c++ATM取款
- VC++环境下如何连接SQL数据库
- 数字图像处理软件源代码三个资源集
- c#与STK开发
- 基于MFC的电梯调度仿真程序
- 局域网下,手机可以和电脑通过sock
- c语言头文件库
- 51单片机C语言编程入门(详讲版)
- mfc连连看代码
- C语言实现的红外发送STM32F10x单片机应
- 21天学通 C++第8版 高清完整带书签PD
- C++实现利用OpenGL实现三维绘图
- ini文件读写操作类C++版
- 基于特征脸的人脸识别MFC+OpenCV
- c++面向对象图书管理系统
- C语言程序设计(谭浩强)最终完整
- C++项目-----仓库管理系统
- cv::namedWindowGLFWwindow以及notepad嵌入MF
- C++&easyx;的坦克大战
- wumpus世界(MFC编写)
- VC++实现两台电脑文件传输(源代码)
- 备忘录--C++程序
- MFC写的对文件内容的DES加解密
- vs2010 MFC基础教程
- C语言参考手册 第五版 PDF扫描无水印
评论
共有 条评论