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.end
return opts;
}(bufKey);
print_bt("opts=", opts);
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(g_collection, bt_filter, opts, NULL);