MongoDB笔记三:C Driver 含日期类型字段document添加和查询,指定document返回的field
来源:互联网 发布:尼康d800调焦软件 编辑:程序博客网 时间:2024/06/02 07:29
Author: kagula
Date: 2017-09-12
Envrionment: [1] VS2013 Update5 [2]MongoDB C Driver 1.7.0
这里只给出重要的函数,上一篇笔记中有的,就不重复贴出来了。
日期类型fidld的插入
time_t timer;time(&timer);tm tmNow;localtime_s(&tmNow, &timer);
bson_t *bt_insert = bson_new();bson_error_t error;BSON_APPEND_UTF8(bt_insert, "date_time", bufLongDate);BSON_APPEND_DATE_TIME(bt_insert, "date_time_tm_type", mktime(&tmNow) * 1000);//日期类型field的插入BSON_APPEND_UTF8(bt_insert, "cable_id", cable_id.c_str());BSON_APPEND_UTF8(bt_insert, "channel_id", channel_id.c_str());BSON_APPEND_INT32(bt_insert, "type_id", type_id);BSON_APPEND_INT32(bt_insert, "data_size", vecData.size());BSON_APPEND_INT32(bt_insert, "element_size", limit);BSON_APPEND_UTF8(bt_insert, "data", pData);BSON_APPEND_DOUBLE(bt_insert, "pt_interval", fInterval);if (!mongoc_collection_insert(g_collection, MONGOC_INSERT_NONE, bt_insert, NULL, &error)) {setError(1);#ifdef _DEBUG_MONGODB_fprintf(stderr, "%s\n", error.message);#endif}
日期类型document的查询,并返回指定字段
bson_t *bt_filter;bt_filter = bson_new();time_t timer;time(&timer);tm tmNow;localtime_s(&tmNow, &timer);bson_t *bt_and = bson_new();BSON_APPEND_ARRAY_BEGIN(bt_filter, "$and", bt_and);//>=bson_t *bt_and_0 = bson_new();BSON_APPEND_DOCUMENT_BEGIN(bt_and, "0", bt_and_0);{bson_t *bt_and_0_gte = bson_new();BSON_APPEND_DOCUMENT_BEGIN(bt_and_0, "date_time_tm_type", bt_and_0_gte);//这个API只能查询最近24天的记录,猜测“参数”超出数据表示范围,或则返回数据太多超出MongoDB默认结果集缓存。BSON_APPEND_DATE_TIME(bt_and_0_gte, "$gte", mktime(&tmNow) * 1000 - 1000 * 60 * 60 * 24 * 24);bson_append_document_end(bt_and_0, bt_and_0_gte);}bson_append_document_end(bt_and, bt_and_0);//<=bson_t *bt_and_1 = bson_new();BSON_APPEND_DOCUMENT_BEGIN(bt_and, "1", bt_and_1);{bson_t *bt_and_1_lte = bson_new();BSON_APPEND_DOCUMENT_BEGIN(bt_and_1, "date_time_tm_type", bt_and_1_lte);BSON_APPEND_DATE_TIME(bt_and_1_lte, "$lte", mktime(&tmNow) * 1000);bson_append_document_end(bt_and_1, bt_and_1_lte);}bson_append_document_end(bt_and, bt_and_1);//==bson_t *bt_and_2 = bson_new();BSON_APPEND_DOCUMENT_BEGIN(bt_and, "2", bt_and_2);{bson_t *bt_and_2_eq = bson_new();BSON_APPEND_DOCUMENT_BEGIN(bt_and_2, "cable_id", bt_and_2_eq);BSON_APPEND_UTF8(bt_and_2_eq, "$eq", cable_id.c_str());bson_append_document_end(bt_and_2, bt_and_2_eq);}bson_append_document_end(bt_and, bt_and_2);bson_append_array_end(bt_filter, bt_and);char bufKey[32];sprintf(bufKey, "data_%d", pos);bson_t *opts = [](char *bufKey)->bson_t *{bson_t *opts;/* order by "date" descending */opts = BCON_NEW("sort", "{", "date_time_tm_type", BCON_INT32(-1), "}");//若要返回的记录数据太多,和记录数量无关,就一条都不返回了//解决办法一:BSON_APPEND_INT32(opts, "limit", 100);//限制返回的记录数量//解决办法二:建立index,db.sampleCollection.createIndex({ cable_id:1, date_time_tm_type : -1 })// 上面的表达式,指的是find参数里的查询条件的表达式用到的field。//解决办法三:采用分页机制。但是不适合我们的情况,我们所有记录还是要的。// 用limit结合skip,可以实现分页。//BSON_APPEND_INT32(opts, "skip", 100);//BSON_APPEND_INT32(opts, "limit", 1000);//方法四:只返回需要的字段,并加index.//select specified fields.begin//如果不用这段代码指定有,默认返回document的所有field。bson_t *bt_projection = bson_new();BSON_APPEND_DOCUMENT_BEGIN(opts, "projection", bt_projection);BSON_APPEND_BOOL(bt_projection, "date_time", true);BSON_APPEND_BOOL(bt_projection, "data_size", true);BSON_APPEND_BOOL(bt_projection, "element_size", true);BSON_APPEND_BOOL(bt_projection, bufKey, true);bson_append_document_end(opts, bt_projection);//select specified fields.endreturn opts;}(bufKey);print_bt("opts=", opts);mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(g_collection, bt_filter, opts, NULL);
阅读全文
0 0
- MongoDB笔记三:C Driver 含日期类型字段document添加和查询,指定document返回的field
- spring mongodb 更新Array中某个指定的document的field的value
- mongodb 更新array内的某个document的field值
- mongodb中查询返回指定字段
- 使用Lucene对图书馆构建Document(文档)和Field(字段)
- Lucene为图书馆构建Document 和 Field
- java MongoDB 字符串日期类型查询和日期类型查询
- 【MongoDB】MongoDB只返回指定的字段的值 和 设定超时项
- mongodb C driver的异步查询
- spring-mongodb返回指定字段
- document.createElement的返回值
- document.body和document.documentElement,document.compatMode的关系
- document和document.body的区别
- document 和 document.all的区别!
- document.getElementsByName和document.getElementById的区别
- document.body 和 document.documentElement 的区别
- document.body和document.documentElement 的区别
- document.body 和 document.documentElement 的区别
- 两个栈实现一个队列-思路
- echarts报表展示
- [caioj 1091] 最小生成树2---prim
- Servlet入门
- linux(ubuntu)环境下安装IDEA
- MongoDB笔记三:C Driver 含日期类型字段document添加和查询,指定document返回的field
- live555二次开发经验总结:RTSPClient客户端与RTSPServer服务器
- 汇编语言实现冒泡算法
- leetcode-14-Longest Common Prefix
- UML图中的依赖、关联、继承、实现、聚合、组合关系的学习
- 面试题Getmemory
- Python fork 操作
- Django教程之八-----编写你的第一个Django应用(7)
- 如何保存python程序执行完毕后的全部变量