MongoDB MapReduce 小例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | var map = function(){ if ( this .gscode == "ZTJB" ){ ymd = this .ymd; emit( "maxymd" , ymd); } } var reduce = function(key, values){ var maxYmd = values[0]; for (var i=1; i<values.length; i++){ if (maxYmd < values[i]){ maxYmd = values[i]; } } return maxYmd; } db.getCollection( 'calcgsdataflash' ).mapReduce( map, reduce, {out:{ inline :1}} ); |
C API 小例子:
官方文档地址:http://mongoc.org/libmongoc/1.8.2/distinct-mapreduce.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | vector<string> vMaxDate; const char * const MAPPER = "function(){" "Date = this.Date;" "emit('maxDate',Date)" "}" ; const char * const REDUCER = "function(key, values){" "var maxDate = values[0];" "for (var i=1; i<values.length; i++){" "if (maxDate < values[i]){" "maxDate = values[i];" "}" "}" "return maxDate;" "}" ; m_mongoDBMgr.MapReduce(MAPPER, REDUCER, vMaxDate); for (vector<string>::iterator iter = vMaxDate.begin(); iter != vMaxDate.end(); iter++) { Json::Reader reader; Json::Value value; if (reader.parse((*iter).c_str(), value)) { iMaxDate = value[ "value" ].asInt(); TLOG_DEBUG( "L2Dynamic iMaxDate:" << iMaxDate << endl); } } //maxYmd bool CMongoDBMgr::MapReduce( const char * const MAPPER, const char * const REDUCER, vector<string> &vData) { TLOG_DEBUG( "begin MapReduce" << endl); bson_t reply; bson_t *command; bson_error_t error; mongoc_cursor_t *cursor; const bson_t *doc; bool map_reduce_done = false ; bool query_done = false ; const char *out_collection_name = "outCollection" ; mongoc_collection_t *out_collection; bson_t find_query = BSON_INITIALIZER; //do MapReduce command = BCON_NEW ( "mapReduce" , BCON_UTF8 (m_sCollection.c_str()), "map" , BCON_CODE (MAPPER), "reduce" , BCON_CODE (REDUCER), "out" , BCON_UTF8 (out_collection_name) ); bool bRet = mongoc_database_command_simple (m_database, command, NULL, &reply, &error); map_reduce_done = true ; if (!bRet) { TLOG_DEBUG( "MapReduce failed:" << error.message << endl); goto cleanup; return false ; } //do query out_collection = mongoc_database_get_collection (m_database, out_collection_name); cursor = mongoc_collection_find_with_opts (out_collection, &find_query, NULL, NULL); query_done = true ; while (mongoc_cursor_next (cursor, &doc)) { char *str; str = bson_as_json(doc, NULL); vData.push_back(str); } if (mongoc_cursor_error (cursor, &error)) { TLOG_DEBUG( "An error occurred:" << error.message << endl); goto cleanup; return false ; } cleanup: if (map_reduce_done) { bson_destroy (&reply); bson_destroy (command); } if (query_done) { mongoc_cursor_destroy(cursor); mongoc_collection_destroy (out_collection); } return true ; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架