MongoDB笔记三:C Driver 含日期类型字段document添加和查询,指定document返回的field
2017-09-12 08:38
956 查看
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);
日期类型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.end
return opts;
}(bufKey);
print_bt("opts=", opts);
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(g_collection, bt_filter, opts, NULL);
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.end
return opts;
}(bufKey);
print_bt("opts=", opts);
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(g_collection, bt_filter, opts, NULL);
相关文章推荐
- mongodb中查询返回指定字段
- spring-data-mongodb查询结果返回指定字段
- spring-data-mongodb查询结果返回指定字段
- MongoDB笔记四: C Driver - 16字节uuid字段的查询和获取
- MongoDB笔记二 - C Driver 添加修改和查询
- laravel-mongodb查询条件与字段类型不一致问题
- mongodb中根据集合中的字段关系返回查询结果
- 通过id查询用户,但是只返回指定的字段
- mybatis查询,返回类型为HashMap,字段为null时没返回
- 每日学习心得:SharePoint 为列表中的文件夹添加子项(文件夹)、新增指定内容类型的子项、查询列表中指定的文件夹下的内容
- hibernate查询数据表char类型字段只返回一个字符
- Mysql复习--添加字段,删除字段,一次多行插入,修改字段名和类型,分组查询,建类似表,
- 博客园现代化建设—[Entity Framework]在LINQ查询中指定返回的字段
- linq查询结果转换为指定字段类型的list集合
- MongoDB查询时排序字段为int类型和string类型的区别
- 返回指定类型格式的日期注解
- MongoDB查询返回指定键
- bat批处理笔记——为指定类型文件添加前缀名
- java操作mongodb,返回某个文档的指定字段
- 博客园现代化建设—[Entity Framework]在LINQ查询中指定返回的字段