项目中遇到了使用mongodb存储软件执行记录的需求,在调用mongodb的c++ driver进行数据查询时,从mongodb中取值的方法如下:
try
{
auto cursor = collection.find({});
for(row : cursor)
{
struct record;
record.ident = QString::fromStdString(bsoncxx::string::to_string(row["ident"].get_utf8().value));
record.startTime = QDateTime::fromMSecsSinceEpoch(row["start_time"].get_int64().value);
record.endTime = QDateTime::fromMSecsSinceEpoch(row["end_time"].get_int64().value);
record.value = row["value"].get_double().value;
}
return true;
}
catch (std::exception e)
{
return false;
}
但是,当mongodb的字段修改后,比如value字段没有了,那么row[“value”]将会抛出异常,但是这种情况下,我希望的是字段不存在则写入一个默认值。
不过如果每一条取值语句都用try…catch包裹起来太过繁杂,于是添加了以下取值模版。
#pragma once
#include <bsoncxx/document/view.hpp>
#include <type_traits>
//bool
template <class T>
typename std::enable_if<std::is_same<bool, T>::value, T>::type bson_get(bsoncxx::document::view view
, const std::string& key
, const T& default = T())
{
try
{
return view[key].get_bool().value;
}
catch (std::exception e)
{
return default;
}
}
//int, int32
template <class T>
typename std::enable_if<std::is_same<int, T>::value, T>::type bson_get(bsoncxx::document::view view
, const std::string& key
, const T& default = T())
{
try
{
return view[key].get_int32().value;
}
catch (std::exception e)
{
return default;
}
}
//int64_t
template <class T>
typename std::enable_if<std::is_same<int64_t, T>::value, T>::type bson_get(bsoncxx::document::view view
, const std::string& key
, const T& default = T())
{
try
{
return view[key].get_int64().value;
}
catch (std::exception e)
{
return default;
}
}
//double
template <class T>
typename std::enable_if<std::is_same<double, T>::value, T>::type bson_get(bsoncxx::document::view view
, const std::string& key
, const T& default = T())
{
try
{
return view[key].get_double().value;
}
catch (std::exception e)
{
return default;
}
}
//std::string
template <class T>
typename std::enable_if<std::is_same<std::string, T>::value, T>::type bson_get(bsoncxx::document::view view
, const std::string& key
, const T& default = T())
{
try
{
return bsoncxx::string::to_string(view[key].get_utf8().value);
}
catch (std::exception e)
{
return default;
}
}
//QString
template <class T>
typename std::enable_if<std::is_same<QString, T>::value, T>::type bson_get(bsoncxx::document::view view
, const std::string& key
, const T& default = T())
{
try
{
auto&& string = bsoncxx::string::to_string(view[key].get_utf8().value);
return QString::fromUtf8(string.c_str(), string.size());
}
catch (std::exception e)
{
return default;
}
}
//QDateTime
template <class T>
typename std::enable_if<std::is_same<QDateTime, T>::value, T>::type bson_get(bsoncxx::document::view view
, const std::string& key
, const T& default = T())
{
try
{
return QDateTime::fromMSecsSinceEpoch(view[key].get_int64());
}
catch (std::exception e)
{
return default;
}
}
这样之前的取值方式就可以修改为:
try
{
auto cursor = collection.find({});
for(row : cursor)
{
struct record;
record.ident = bson_get<QString>(row, "ident");
record.startTime = bson_get<QDateTime>(row, "start_time");
record.endTime = bson_get<QDateTime>(row, "end_time");
record.value = bson_get<double>(row, "value", 0.0);
}
return true;
}
catch (std::exception e)
{
return false;
}
这样在value字段不存在时,record就会获得一个默认值0.0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。