Hey sorry couldn’t get back to you early.
Although i tried what you said(checked again, did the whole process again)…but it didnt work atleast for my project which i made just to test the release mode.
For my project where i actually want to use mongoDB. I did check the config files of it, did the process again, and it worked, atleast the project ran in the release mode…but only for few seconds. It failed in my mongoDB handler class. The instance of mongoDB is being created using a singleton class(as i want to use this handler class in various different places and want to see if the instance is already created or not).
The error happens on line 7 of MongoDB.cpp file
conn = mongocxx::client(mongoURI, client_options);
Error says :
1. A breakpoint instruction (__debugbreak() statement or a similar call) was executed in Project.exe.
2. Exception thrown at 0x00007FFCBF916F95 (ntdll.dll) in Project.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
3. Unhandled exception at 0x00007FFCBF96F449 (ntdll.dll) in Project.exe: 0xC0000374: A heap has been corrupted (parameters: 0x00007FFCBF9D97F0).
MongoDB.h file
#pragma once
#include <cstdint>
#include <string>
#include <iostream>
#include "../Header Files/Config.h"
#include "bsoncxx/builder/stream/document.hpp"
#include "bsoncxx/json.hpp"
#include "bsoncxx/oid.hpp"
#include "mongocxx/client.hpp"
#include "mongocxx/database.hpp"
#include "mongocxx/uri.hpp"
#include <mongocxx/exception/operation_exception.hpp>
namespace learning {
const mongocxx::uri mongoURI(mongodb_uri);
const std::string dbName = databaseName;
const std::string collName = colectionName;
class MongoDB {
public:
MongoDB();
void insertDocument(const bsoncxx::document::value document);
std::tuple<std::string, std::string, std::string> findDocument(const std::string& value);
bool isDataPresent(const std::string& key, const std::string& value);
int findScore(const std::string& value);
void updateDocument(const std::string& key, const int& value, const std::string& newKey, const int& newValue);
std::vector<std::pair<std::string, int>> getTopScores(int limit);
private:
mongocxx::options::client client_options;
mongocxx::options::server_api api;
mongocxx::client conn;
mongocxx::v_noabi::database ammpedUPDB;
mongocxx::v_noabi::collection loginInfoCollection;
};
}
MongoDB.cpp file
#include "../Header Files/MongoDB.h"
learning::MongoDB::MongoDB() :
api(mongocxx::options::server_api::version::k_version_1)
{
client_options.server_api_opts(api);
conn = mongocxx::client(mongoURI, client_options);
ammpedUPDB = conn.database(dbName);
loginInfoCollection = ammpedUPDB.collection(collName);
}
void learning::MongoDB::insertDocument(const bsoncxx::document::value document)
{
loginInfoCollection.insert_one(document.view());
}
std::tuple<std::string, std::string, std::string> learning::MongoDB::findDocument(const std::string& value)
{
std::string key;
if (value.find('@') != std::string::npos) {
// Contains '@' symbol, so it looks like an email
key = "email";
}
else {
// Doesn't contain '@', so it looks like a username
key = "username";
}
auto filter = bsoncxx::builder::stream::document{} << key << value << bsoncxx::builder::stream::finalize;
// Add query filter argument in find
auto cursor = loginInfoCollection.find({ filter });
if (cursor.begin() == cursor.end()) {
return { "", "", "" };; // No data found
}
// Extract the first document from the cursor
auto document = *cursor.begin();
// Extract the individual components of the retrieved data
std::string retrievedUsername = std::string(document["username"].get_string().value);
std::string retrievedEmail = std::string(document["email"].get_string().value);
std::string retrievedPassword = std::string(document["pwd"].get_string().value);
return { retrievedUsername, retrievedEmail, retrievedPassword };
}
bool learning::MongoDB::isDataPresent(const std::string& key, const std::string& value)
{
// Create the query filter
auto filter = bsoncxx::builder::stream::document{} << key << value << bsoncxx::builder::stream::finalize;
// Add query filter argument in find
auto cursor = loginInfoCollection.find({ filter });
return (cursor.begin() != cursor.end());
}
int learning::MongoDB::findScore(const std::string& value)
{
auto filter = bsoncxx::builder::stream::document{} << "username" << value << bsoncxx::builder::stream::finalize;
mongocxx::options::find opts;
opts.projection(bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("score", 1)));
auto cursor = loginInfoCollection.find({ filter }, opts);
for (auto&& doc : cursor)
{
bsoncxx::document::element curvename = doc["score"];
int score = curvename.get_int32().value;
return score;
}
}
void learning::MongoDB::updateDocument(const std::string& key, const int& value, const std::string& newKey, const int& newValue)
{
loginInfoCollection.update_one(bsoncxx::builder::stream::document{} << key << value << bsoncxx::builder::stream::finalize,
bsoncxx::builder::stream::document{} << "$set" << bsoncxx::builder::stream::open_document << newKey << newValue << bsoncxx::builder::stream::close_document << bsoncxx::builder::stream::finalize);
}
std::vector<std::pair<std::string, int>> learning::MongoDB::getTopScores(int limit)
{
std::vector<std::pair<std::string, int>> topScores;
mongocxx::options::find opts;
opts.sort(bsoncxx::builder::basic::make_document(
bsoncxx::builder::basic::kvp("score", -1)));
opts.limit(limit);
auto cursor = loginInfoCollection.find({}, opts);
for (auto&& doc : cursor) {
std::string username = std::string(doc["username"].get_string().value);
int score = doc["score"].get_int32().value;
topScores.emplace_back(username, score);
}
return topScores;
}