MongoDB C++ gridfs worked example
2023-09-14 09:11:56 时间
使用libmongoc,参考:http://mongoc.org/libmongoc/current/mongoc_gridfs_t.html
#include <mongoc.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> class MongoGridFS { public: MongoGridFS(const char* db); ~MongoGridFS(); void saveFile(const char* input_file_path, const char* filename); private: mongoc_gridfs_t *gridfs; mongoc_client_t *client; }; MongoGridFS::MongoGridFS(const char* db) { assert(db != NULL); mongoc_init (); /* connect to localhost client */ client = mongoc_client_new ("mongodb://127.0.0.1:27017?appname=gridfs-example"); assert (client); mongoc_client_set_error_api (client, 2); /* grab a gridfs handle in test prefixed by fs */ bson_error_t error; gridfs = mongoc_client_get_gridfs (client, db, "fs", &error); assert (gridfs); } void MongoGridFS::saveFile(const char* input_file_path, const char* filename) { assert(input_file_path != NULL && filename != NULL); mongoc_stream_t *stream = mongoc_stream_file_new_for_path (input_file_path, O_RDONLY, 0); assert (stream); mongoc_gridfs_file_opt_t opt = {0}; opt.filename = filename; /* the driver generates a file_id for you */ mongoc_gridfs_file_t *file = mongoc_gridfs_create_file_from_stream (gridfs, stream, &opt); assert (file); mongoc_gridfs_file_save (file); mongoc_gridfs_file_destroy (file); } MongoGridFS::~MongoGridFS() { mongoc_gridfs_destroy (gridfs); mongoc_client_destroy (client); mongoc_cleanup (); } int main (int argc, char *argv[]) { MongoGridFS gridfs("test2gridfs"); gridfs.saveFile("test.py", "test.py"); return 0; }
相关文章
- C++单例模式的问题
- 经典中的品味:第一章 C++的Hello,World!
- C++ code:动态内存
- C语言/C++常见习题问答集锦(七十七)之玫瑰花数
- VC++技术内幕(第四版)笔记--SetWindowExt和SetViewportExt
- [MongoDB] Mongodb攻略
- Atitit codeblock c++开发环境建立attilax总结
- paip.java c++得到当前类,方法名称以及行号
- 【华为OD机试 2023】 快速开租建站(C++ Java JavaScript Python)
- Node.js使用mongodb.js操作MongoDB数据库
- 【MongoDB】The Access control of mongodb
- c++ vector C++ vector存放结构体 并且排序
- C++多态的概念及前提条件(最精辟)
- VC++时间函数的使用(附源码)
- 机器人C++库(9)Robotics Library 之机器人建模(VRML)、工作场景Scances建模(VRML)
- PCL 统计滤波(C++详细过程版)
- C++从入门到进阶的系列书籍推荐