MongoDB C++ Driver取值模版

本文介绍了一种在C++中使用MongoDB时,针对字段可能缺失的情况,通过模板函数实现异常处理的方法,避免了繁琐的try...catch语句,提高了代码的健壮性和可读性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中遇到了使用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

使用C++20更加简洁:

#include <bsoncxx/document/view.hpp>

#include <type_traits>
#include <concepts>

#include <QString>
#include <QDateTime>

template <typename T>
concept Ret = std::same_as<T, int>
	|| std::same_as<T, bool>
	|| std::same_as<T, int64_t>
	|| std::same_as<T, std::string>
	|| std::same_as<T, double>
	|| std::same_as<T, QString>
	|| std::same_as<T, QDateTime>;

template <Ret T>
auto bson_get(bsoncxx::document::view view
	, const std::string& key
	, const T& default = T())
{
	try
	{
		if constexpr (std::same_as<T, bool>)
			return view[key].get_bool().value;
		else if constexpr (std::same_as<T, QString>)
			return QString::fromUtf8(view[key].get_utf8().value.data());
		else if constexpr (std::same_as<T, int>)
			return view[key].get_int32().value;
		else if constexpr (std::same_as<T, int64_t>)
			return view[key].get_int64().value;
		else if constexpr (std::same_as<T, QDateTime>)
			return QDateTime::fromMSecsSinceEpoch(view[key].get_int64().value);
		else if constexpr (std::same_as<T, double>)
			return view[key].get_double().value;
		else if constexpr (std::same_as<T, std::string>)
			return std::string(view[key].get_utf8().value);
		else
			static_assert(false,"get value failed");
	}
	catch (std::exception e)
	{
		return default;
	}
}
### 如何在 Visual Studio 中配置和使用 MongoDB C++ 驱动 #### 准备工作 为了能够在 Visual Studio 中成功配置并使用 MongoDB C++ 驱动,需先完成若干准备工作。确保已安装最新版本的 Visual Studio 并具备必要的开发组件[^3]。 #### 获取驱动源码与依赖项 从 GitHub 上获取 `mongo-c-driver` 的发布版,这是构建 C++ 驱动的基础[^5]。解压后按照官方文档指示操作,在命令提示符环境下执行特定指令来生成解决方案文件: ```shell cd path\to\mongo-c-driver mkdir build && cd build "C:\Program Files (x86)\CMake\bin\cmake.exe" -G "Visual Studio 14 2015 Win64" .. ``` 这一步骤完成后会在当前目录下创建名为 `mongo-c-driver.sln` 的解决方案文件,可以直接通过 Visual Studio 打开它来进行后续编译过程[^2]。 #### 设置环境变量 对于 Windows 用户来说,设置好相应的环境路径非常重要。将 `libmongoc` 和 `libbson` 库所在的 bin 文件夹加入到系统的 PATH 变量中去,以便于链接阶段能够找到所需的动态链接库(DLL)[^4]。 #### 创建新项目集成MongoDB C++ Driver 当上述步骤都顺利完成之后,就可以着手建立一个新的 C++ 控制台应用程序工程了。接着调整项目的属性页如下所示: - **C/C++ -> General -> Additional Include Directories**: 添加 `mongo-cxx-driver/include/bsoncxx/v_noabi/`, `mongo-cxx-driver/include/mongocxx/v_noabi/`. - **Linker -> General -> Additional Library Directories**: 输入 `mongo-cxx-driver/build/src/lib`. 最后记得把静态或共享库(`*.lib`)也添加进来作为附加依赖项之一,通常位于刚才指定的那个文件夹里边。 #### 测试连接数据库功能 下面是一段简单的测试代码用来验证整个流程是否无误地完成了配置: ```cpp #include <iostream> #include <mongocxx/client.hpp> #include <mongocxx/stdx.hpp> #include <mongocxx/uri.hpp> int main() { try { mongocxx::client client{mongocxx::uri{}}; std::cout << "Connected to MongoDB successfully!" << std::endl; } catch (const std::exception& e) { std::cerr << "Failed to connect: " << e.what() << '\n'; return EXIT_FAILURE; } } ``` 如果一切正常的话,则会看到控制台上打印出成功的消息;反之则可能需要回溯之前的每一步仔细排查潜在的问题所在[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值