Mongodb c++ dll compile error

I currently develop an C++ dll with vs2022 and mongo-cxx-driver-r3.9.0 running in win10. I think I have successfully installed the driver since I have successfully to develop my first mongo console app with vs2022. However, it is not for my current dll project. The error from VS2022 is showed below. Please kindly help to solve ‘>C:\VS_sln\Mongo_test\Mongo_test\include\bsoncxx\v_noabi\bsoncxx\builder\basic\impl.hpp(50,9): error C2668: ‘bsoncxx::v_noabi::builder::core::append’: ambiguous call to overloaded function
1>C:\VS_sln\Mongo_test\Mongo_test\include\bsoncxx\v_noabi\bsoncxx\builder\core.hpp(569,11): message : could be ‘bsoncxx::v_noabi::builder::core &bsoncxx::v_noabi::builder::core::append(int64_t)’
1>C:\VS_sln\Mongo_test\Mongo_test\include\bsoncxx\v_noabi\bsoncxx\builder\core.hpp(556,11): message : or ‘bsoncxx::v_noabi::builder::core &bsoncxx::v_noabi::builder::core::append(int32_t)’
1>C:\VS_sln\Mongo_test\Mongo_test\include\bsoncxx\v_noabi\bsoncxx\builder\core.hpp(543,11): message : or ‘bsoncxx::v_noabi::builder::core &bsoncxx::v_noabi::builder::core::append(double)’
1>C:\VS_sln\Mongo_test\Mongo_test\include\bsoncxx\v_noabi\bsoncxx\builder\core.hpp(530,11): message : or ‘bsoncxx::v_noabi::builder::core &bsoncxx::v_noabi::builder::core::append(bool)’
1>C:\VS_sln\Mongo_test\Mongo_test\include\bsoncxx\v_noabi\bsoncxx\builder\basic\impl.hpp(49,1): message : while trying to match the argument list ‘(unsigned int)’
1>C:\VS_sln\Mongo_test\Mongo_test\include\bsoncxx\v_noabi\bsoncxx\builder\basic\impl.hpp(54): message : see reference to function template instantiation ‘void bsoncxx::v_noabi::builder::basic::impl::generic_append<unsigned int&,void,void>(bsoncxx::v_noabi::builder::core *,T)’ being compiled
1> with
1> [
1> T=unsigned int &
1> ]’

Hi @Terence_Tung
Can you share the code which is generating this error?

Sure. The code is below:-
#define NOMINMAX
#include “sierrachart.h”
#include “windows.h”
#include < algorithm>
#include < iostream>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/stdx.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/document/value.hpp>
#include <bsoncxx/json.hpp>
using namespace std;
using namespace mongocxx;
using namespace bsoncxx;

using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_array;
using bsoncxx::builder::basic::make_document;

SCDLLName(“Mongodb_test”)

SCSFExport scsf_Mongodb_test(SCStudyInterfaceRef sc)
{
SCInputRef NumBarsRef = sc.Input[0];
SCInputRef RefreshDB = sc.Input[1];
SCInputRef Doji_Ref = sc.Input[2];
SCInputRef LTrade_Ref = sc.Input[3];
SCInputRef AskBid_diff_O = sc.Input[4];
SCInputRef AskBid_diff_H = sc.Input[5];
SCInputRef AskBid_diff_L = sc.Input[6];
SCInputRef AskBid_diff_C = sc.Input[7];
SCInputRef POC_Value = sc.Input[8];
SCInputRef Cum_Vol = sc.Input[9];
SCInputRef UpdateFreq = sc.Input[10];
SCInputRef DBFile = sc.Input[11];

SCSubgraphRef Open = sc.Subgraph[0];
SCSubgraphRef High = sc.Subgraph[1];
SCSubgraphRef Low = sc.Subgraph[2];
SCSubgraphRef Last = sc.Subgraph[3];
if (sc.SetDefaults)
{
	// Set the study configuration and defaults.

	sc.GraphName = "SC MongoDB 240101 10:05";
	NumBarsRef.Name = "Number of Bars back ref";
	NumBarsRef.SetInt(500);

	Doji_Ref.Name = "RS Doji Ref.";
	Doji_Ref.SetStudySubgraphValues(8, 0);

	LTrade_Ref.Name = "LT Trade Ref.";
	LTrade_Ref.SetStudySubgraphValues(10, 0);

	AskBid_diff_O.Name = "Ask/Bid diff Open";
	AskBid_diff_O.SetStudySubgraphValues(19, 0);

	AskBid_diff_H.Name = "Ask/Bid diff High";
	AskBid_diff_H.SetStudySubgraphValues(19, 1);

	AskBid_diff_L.Name = "Ask/Bid diff Low";
	AskBid_diff_L.SetStudySubgraphValues(19, 2);

	AskBid_diff_C.Name = "Ask/Bid diff Close";
	AskBid_diff_C.SetStudySubgraphValues(19, 3);

	POC_Value.Name = "POC Value";
	POC_Value.SetStudySubgraphValues(9, 0);

	Cum_Vol.Name = "Cumulative Volume";
	Cum_Vol.SetStudySubgraphValues(11, 4);


	RefreshDB.Name = " Refresh DB Manual/Auto";
	RefreshDB.SetCustomInputStrings("Once;Continous;Stop");
	RefreshDB.SetCustomInputIndex(2);

	UpdateFreq.Name = "Update frequemcy (Sec)";
	UpdateFreq.SetInt(60);

	DBFile.Name = "DB Filename";
	DBFile.SetString("test_SC2.db");

	sc.AutoLoop = 1;
	sc.GraphRegion = 0;

	// During development set this flag to 1, so the DLL can be modified. When development is completed, set it to 0 to improve performance.
	sc.FreeDLL = 0;
	//sc.MaintainAdditionalChartDataArrays = 1;
	sc.MaintainVolumeAtPriceData = 1;
	sc.CalculationPrecedence = LOW_PREC_LEVEL;
	return;
}

SCDateTime& LastAdjustmentDateTime = sc.GetPersistentSCDateTime(1);
int& r_Update = sc.GetPersistentInt(1);

if (sc.ChartIsDownloadingHistoricalData(sc.ChartNumber) || sc.IsFullRecalculation)
	return;
SCFloatArray Doji_ary, LTrade_ary, AskBid_O_ary, AskBid_H_ary, AskBid_L_ary, AskBid_C_ary, POC_ary, Renko_O_ary, Renko_H_ary, Renko_L_ary, Renko_C_ary, CumV_ary;

sc.GetStudyArrayUsingID(Doji_Ref.GetStudyID(), Doji_Ref.GetSubgraphIndex(), Doji_ary);
sc.GetStudyArrayUsingID(LTrade_Ref.GetStudyID(), LTrade_Ref.GetSubgraphIndex(), LTrade_ary);
sc.GetStudyArrayUsingID(AskBid_diff_O.GetStudyID(), AskBid_diff_O.GetSubgraphIndex(), AskBid_O_ary);
sc.GetStudyArrayUsingID(AskBid_diff_H.GetStudyID(), AskBid_diff_H.GetSubgraphIndex(), AskBid_H_ary);
sc.GetStudyArrayUsingID(AskBid_diff_L.GetStudyID(), AskBid_diff_L.GetSubgraphIndex(), AskBid_L_ary);
sc.GetStudyArrayUsingID(AskBid_diff_C.GetStudyID(), AskBid_diff_C.GetSubgraphIndex(), AskBid_C_ary);
sc.GetStudyArrayUsingID(POC_Value.GetStudyID(), POC_Value.GetSubgraphIndex(), POC_ary);
sc.GetStudyArrayUsingID(Cum_Vol.GetStudyID(), Cum_Vol.GetSubgraphIndex(), CumV_ary);

if (Doji_ary.GetArraySize() <= 0 || LTrade_ary.GetArraySize() <= 0 || AskBid_O_ary.GetArraySize() <= 0 || AskBid_H_ary.GetArraySize() <= 0
	|| AskBid_L_ary.GetArraySize() <= 0 || AskBid_C_ary.GetArraySize() <= 0 || POC_ary.GetArraySize() <= 0 || CumV_ary.GetArraySize() <= 0) {
	sc.AddMessageToLog("Ref subgraph data invalid", 0);
	return;
}
int Update = 0;

if (RefreshDB.GetIndex() == 0)
	Update = 1;
else if (RefreshDB.GetIndex() == 1) {
	if (sc.CurrentSystemDateTime - LastAdjustmentDateTime > SCDateTime::SECONDS(UpdateFreq.GetInt()))
		Update = 1;
	else
		Update = 0;
}

int Index = 0;
SCString  LogText;
SCString sym_collect = DBFile.GetString();
SCString sym = sym_collect.Left(3);

mongocxx::instance inst{};
mongocxx::uri uri("mongodb://localhost:27017");
mongocxx::client conn(uri);

auto collection = conn[sym.GetChars()][sym_collect.GetChars()];

for (Index = 0; Index < sc.ArraySize; Index++) {
	float OpenVal = sc.Open[Index];
	float HighVal = sc.High[Index];
	float LowVal = sc.Low[Index];
	float CloseVal = sc.Close[Index];


	Last[Index] = (OpenVal + HighVal + LowVal + CloseVal) / 4.0f;

	if (Index == 0) {
		Open[Index] = OpenVal;
		//LogText.Format("Open[0]: %f", OpenVal);
		//sc.AddMessageToLog(LogText, 0);
	}
	else {
		Open[Index] = (Open[Index - 1] + Last[Index - 1]) / 2.0f;
	}

	High[Index] = max(HighVal, Open[Index]);
	Low[Index] = min(LowVal, Open[Index]);
}
if (Update > 0 && sc.Index == sc.ArraySize - 1) {

	int BarIndex;
	double StartT;
	double EndT;

	float Renko_O_ary;
	float Renko_H_ary;
	float Renko_L_ary;
	float Renko_C_ary;

	auto order = bsoncxx::builder::stream::document{} << "_id" << -1 << finalize;
	auto field = bsoncxx::builder::stream::document{} << "_id" << 0 << "starttime" << 1 << finalize;
	mongocxx::options::find opts;
	opts.sort(order.view()).projection(field.view()).limit(1);

	auto result = collection.find_one({}, opts);
	bsoncxx::document::element ans = result->view()["starttime"];
	double LastBarTime = ans.get_double().value;

	s_VolumeAtPriceV2 VolumeAtPrice;
	s_VolumeLevelAtPrice* p_VolumeLevelAtPrice = nullptr;
	//vector<	pair<unsigned int, float >> PV_Pair;
	//Backfill past 10 bars after 10 update
	if (r_Update >= 10) {
		for (BarIndex = sc.ArraySize - 10; BarIndex < sc.ArraySize - 1; BarIndex++) {
			unsigned int MaxTradeVol = 0;
			float MaxTradePrice = 0.0f;

			if (LTrade_ary[BarIndex] > 0) {
				int VAPSizeAtBarIndex = sc.p_VolumeLevelAtPriceForBars->GetSizeAtBarIndex(BarIndex);

				for (int VAPIndex = 0; VAPIndex < VAPSizeAtBarIndex; VAPIndex++) {
					if (!sc.p_VolumeLevelAtPriceForBars->GetVAPElementAtIndex(BarIndex, VAPIndex, &p_VolumeLevelAtPrice))
						break;
					unsigned int maxTradeVolAtPrice = sc.p_VolumeLevelAtPriceForBars->GetMaxVolumeAtPrice(BarIndex, p_VolumeLevelAtPrice->PriceInTicks);
					if (maxTradeVolAtPrice > MaxTradeVol) {
						MaxTradeVol = maxTradeVolAtPrice;
						MaxTradePrice = sc.TicksToPriceValue(p_VolumeLevelAtPrice->PriceInTicks);
					}
				}
			}
			auto rc_update = collection.update_one(make_document(kvp("starttime", StartT)),
				make_document(kvp("$set", make_document(
					kvp("endtime", EndT),
					kvp("doji", (int)Doji_ary[BarIndex]),
					kvp("POC", POC_ary[BarIndex]),
					kvp("LTV", MaxTradeVol),
					kvp("LTP", MaxTradePrice),
					kvp("cumvol", (unsigned int)CumV_ary[BarIndex]),
					kvp("delta1", (int)AskBid_O_ary[BarIndex]),
					kvp("delta2", (int)AskBid_H_ary[BarIndex]),
					kvp("delta3", (int)AskBid_L_ary[BarIndex]),
					kvp("delta4", (int)AskBid_C_ary[BarIndex]),
					kvp("Renko_O", Renko_O_ary),
					kvp("Renko_H", Renko_H_ary),
					kvp("Renko_L", Renko_L_ary),
					kvp("Renko_C", Renko_C_ary),
					kvp("vol", (unsigned int)sc.Volume[BarIndex]),
					kvp("LastP", sc.LastTradePrice)
				))));
		}
		r_Update = 0;
	}

	for (BarIndex = max(sc.ArraySize - NumBarsRef.GetInt(), 0); BarIndex < sc.ArraySize; BarIndex++) {

		StartT = sc.BaseDateTimeIn[BarIndex].GetAsDouble();
		EndT = sc.BaseDataEndDateTime[BarIndex].GetAsDouble();


		Renko_O_ary = sc.RoundToTickSize(Open[BarIndex]);
		Renko_H_ary = sc.RoundToTickSize(High[BarIndex]);
		Renko_L_ary = sc.RoundToTickSize(Low[BarIndex]);
		Renko_C_ary = sc.RoundToTickSize(Last[BarIndex]);

		//LogText.Format("Open value: %f High value: %f Low value: %f Last value: %f", Renko_O_ary, Renko_H_ary, Renko_L_ary, Renko_C_ary);
		//sc.AddMessageToLog(LogText, 0);
		//LogText.Format("Open value: %f High value: %f Low value: %f Last value: %f", sc.Open[BarIndex], sc.High[BarIndex], sc.Low[BarIndex], sc.Close[BarIndex]);
		//sc.AddMessageToLog(LogText, 0);

		bool update_lastbar = FALSE;;
		if (sc.FormattedEvaluateUsingDoubles(StartT, 6, LESS_OPERATOR, LastBarTime, 6) == 1)
			continue;
		else if (sc.FormattedEvaluateUsingDoubles(StartT, 6, EQUAL_OPERATOR, LastBarTime, 6) == 1)
			update_lastbar = TRUE;
		else
			update_lastbar = FALSE;

		unsigned int MaxTradeVol = 0;
		float MaxTradePrice = 0.0f;

		if (LTrade_ary[BarIndex] > 0) {
			int VAPSizeAtBarIndex = sc.p_VolumeLevelAtPriceForBars->GetSizeAtBarIndex(BarIndex);

			for (int VAPIndex = 0; VAPIndex < VAPSizeAtBarIndex; VAPIndex++) {
				if (!sc.p_VolumeLevelAtPriceForBars->GetVAPElementAtIndex(BarIndex, VAPIndex, &p_VolumeLevelAtPrice))
					break;
				unsigned int maxTradeVolAtPrice = sc.p_VolumeLevelAtPriceForBars->GetMaxVolumeAtPrice(BarIndex, p_VolumeLevelAtPrice->PriceInTicks);
				if (maxTradeVolAtPrice > MaxTradeVol) {
					MaxTradeVol = maxTradeVolAtPrice;
					MaxTradePrice = sc.TicksToPriceValue(p_VolumeLevelAtPrice->PriceInTicks);
				}
			}
		}
		if (!update_lastbar) {
			auto rc_insert = collection.insert_one(make_document(
				kvp("starttime", StartT),
				kvp("endtime", EndT),
				kvp("doji", (int)Doji_ary[BarIndex]),
				kvp("POC", POC_ary[BarIndex]),
				kvp("LTV", MaxTradeVol),
				kvp("LTP", MaxTradePrice),
				kvp("cumvol", (unsigned int)CumV_ary[BarIndex]),
				kvp("delta1", (int)AskBid_O_ary[BarIndex]),
				kvp("delta2", (int)AskBid_H_ary[BarIndex]),
				kvp("delta3", (int)AskBid_L_ary[BarIndex]),
				kvp("delta4", (int)AskBid_C_ary[BarIndex]),
				kvp("Renko_O", Renko_O_ary),
				kvp("Renko_H", Renko_H_ary),
				kvp("Renko_L", Renko_L_ary),
				kvp("Renko_C", Renko_C_ary),
				kvp("vol", (unsigned int)sc.Volume[BarIndex]),
				kvp("LastP", sc.LastTradePrice)
			));
		}
		else {
			auto rc_update = collection.update_one(make_document(kvp("starttime", StartT)),
				make_document(kvp("$set", make_document(
					kvp("endtime", EndT),
					kvp("doji", (int)Doji_ary[BarIndex]),
					kvp("POC", POC_ary[BarIndex]),
					kvp("LTV", MaxTradeVol),
					kvp("LTP", MaxTradePrice),
					kvp("cumvol", (unsigned int)CumV_ary[BarIndex]),
					kvp("delta1", (int)AskBid_O_ary[BarIndex]),
					kvp("delta2", (int)AskBid_H_ary[BarIndex]),
					kvp("delta3", (int)AskBid_L_ary[BarIndex]),
					kvp("delta4", (int)AskBid_C_ary[BarIndex]),
					kvp("Renko_O", Renko_O_ary),
					kvp("Renko_H", Renko_H_ary),
					kvp("Renko_L", Renko_L_ary),
					kvp("Renko_C", Renko_C_ary),
					kvp("vol", (unsigned int)sc.Volume[BarIndex]),
					kvp("LastP", sc.LastTradePrice)
				))));
		}

	}


	if (RefreshDB.GetIndex() == 0) RefreshDB.SetCustomInputIndex(2);
	LastAdjustmentDateTime = sc.CurrentSystemDateTime;
	r_Update = r_Update + 1;



}

}

The error message suggests ambiguity in the append (MongoDB C++ Driver: bsoncxx::v_noabi::builder::core Class Reference) function calls, likely because the compiler is unable to deduce the correct overload of append due to different data types provided. To resolve this issue, you might need to explicitly specify the data types. See examples here - https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/bsoncxx/builder_basic.cpp#L64

Thank you Rishabh. I finally compile successfully after adding the bson data types. However my dll run with cpu exception error when run to the code @" auto result = collection.find_one({}, opts); " which has no problem when I run in my console apps. Do you have any idea to solve it?

Could you wrap your code in a try catch block and see if it is generating an exception? See example below

try
{
// Your code here..
}
catch (const std::exception& e) 
{
    std::cout<< "Exception: " << e.what() << std::endl;
}

I put the codes into the try{} but nothing was caught in the catch{}. What is next I can do? Please advise.

Can you share a screenshot of the error with the callstack?