|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <mongocxx/client.hpp> |
| 4 | +#include <bsoncxx/builder/stream/document.hpp> |
| 5 | +#include <bsoncxx/json.hpp> |
| 6 | +#include <mongocxx/uri.hpp> |
| 7 | +#include <mongocxx/instance.hpp> |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | +#include <iostream> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +using namespace std; |
| 14 | + |
| 15 | +std::string getEnvironmentVariable(std::string environmentVarKey) |
| 16 | +{ |
| 17 | + char* pBuffer = nullptr; |
| 18 | + size_t size = 0; |
| 19 | + auto key = environmentVarKey.c_str(); |
| 20 | + |
| 21 | + // Use the secure version of getenv, ie. _dupenv_s to fetch environment variable. |
| 22 | + if (_dupenv_s(&pBuffer, &size, key) == 0 && pBuffer != nullptr) |
| 23 | + { |
| 24 | + std::string environmentVarValue(pBuffer); |
| 25 | + free(pBuffer); |
| 26 | + return environmentVarValue; |
| 27 | + } |
| 28 | + else |
| 29 | + { |
| 30 | + return ""; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +auto mongoURIStr = getEnvironmentVariable("MONGODB_URI"); |
| 35 | +static const mongocxx::uri mongoURI = mongocxx::uri{ mongoURIStr }; |
| 36 | + |
| 37 | + |
| 38 | +// ********************************************** Database Methods ********************************************** |
| 39 | + |
| 40 | +// Get all the databases from a given client. |
| 41 | +vector<string> getDatabases(mongocxx::client& client) |
| 42 | +{ |
| 43 | + return client.list_database_names(); |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +// ********************************************** Collection Methods ********************************************** |
| 48 | + |
| 49 | +// Create a new collection in the given database. |
| 50 | +void createCollection(mongocxx::database& db, const string& collectionName) |
| 51 | +{ |
| 52 | + db.create_collection(collectionName); |
| 53 | +} |
| 54 | + |
| 55 | +// Print the contents of the given collection. |
| 56 | +void printCollection(mongocxx::collection& collection) |
| 57 | +{ |
| 58 | + // Check if collection is empty. |
| 59 | + if (collection.count_documents({}) == 0) |
| 60 | + { |
| 61 | + cout << "Collection is empty." << endl; |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + auto cursor = collection.find({}); |
| 66 | + |
| 67 | + for (auto&& doc : cursor) |
| 68 | + { |
| 69 | + cout << bsoncxx::to_json(doc) << endl; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// ********************************************** Document Methods ********************************************** |
| 74 | + |
| 75 | +// Create a document from the given key-value pairs. |
| 76 | +bsoncxx::document::value createDocument(const vector<pair<string, string>>& keyValues) |
| 77 | +{ |
| 78 | + bsoncxx::builder::stream::document document{}; |
| 79 | + |
| 80 | + for (auto& keyValue : keyValues) |
| 81 | + { |
| 82 | + document << keyValue.first << keyValue.second; |
| 83 | + } |
| 84 | + |
| 85 | + return document << bsoncxx::builder::stream::finalize; |
| 86 | +} |
| 87 | + |
| 88 | +// Insert a document into the given collection. |
| 89 | +void insertDocument(mongocxx::collection& collection, const bsoncxx::document::value& document) |
| 90 | +{ |
| 91 | + collection.insert_one(document.view()); |
| 92 | +} |
| 93 | + |
| 94 | +// Delete a document from a given collection. |
| 95 | +void deleteDocument(mongocxx::collection& collection, const bsoncxx::document::value& document) |
| 96 | +{ |
| 97 | + collection.delete_one(document.view()); |
| 98 | +} |
| 99 | + |
| 100 | +// Update the document with given key-value pair. |
| 101 | +void updateDocument(mongocxx::collection& collection, const string& key, const string& value, const string& newKey, const string& newValue) |
| 102 | +{ |
| 103 | + collection.update_one(bsoncxx::builder::stream::document{} << key << value << bsoncxx::builder::stream::finalize, |
| 104 | + bsoncxx::builder::stream::document{} << "$set" << bsoncxx::builder::stream::open_document << newKey << newValue << bsoncxx::builder::stream::close_document << bsoncxx::builder::stream::finalize); |
| 105 | +} |
| 106 | + |
| 107 | +// Find the document with given key-value pair. |
| 108 | +void findDocument(mongocxx::collection& collection, const string& key, const string& value) |
| 109 | +{ |
| 110 | + // Create the query filter |
| 111 | + auto filter = bsoncxx::builder::stream::document{} << key << value << bsoncxx::builder::stream::finalize; |
| 112 | + |
| 113 | + //Add query filter argument in find |
| 114 | + auto cursor = collection.find({ filter }); |
| 115 | + |
| 116 | + for (auto&& doc : cursor) |
| 117 | + { |
| 118 | + cout << bsoncxx::to_json(doc) << endl; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | + |
| 123 | +// ********************************************** I/O Methods ********************************************** |
| 124 | + |
| 125 | +// Input student record. |
| 126 | +void inputStudentRecord(mongocxx::collection& collection) |
| 127 | +{ |
| 128 | + string name, rollNo, branch, year; |
| 129 | + |
| 130 | + cout << "Enter name: "; |
| 131 | + cin >> name; |
| 132 | + |
| 133 | + cout << "Enter roll number: "; |
| 134 | + cin >> rollNo; |
| 135 | + |
| 136 | + cout << "Enter branch: "; |
| 137 | + cin >> branch; |
| 138 | + |
| 139 | + cout << "Enter year: "; |
| 140 | + cin >> year; |
| 141 | + |
| 142 | + insertDocument(collection, createDocument({ {"name", name}, {"rollNo", rollNo}, {"branch", branch}, {"year", year} })); |
| 143 | +} |
| 144 | + |
| 145 | +// Update student record. |
| 146 | +void updateStudentRecord(mongocxx::collection& collection) |
| 147 | +{ |
| 148 | + string rollNo, newBranch, newYear; |
| 149 | + |
| 150 | + cout << "Enter roll number: "; |
| 151 | + cin >> rollNo; |
| 152 | + |
| 153 | + cout << "Enter new branch: "; |
| 154 | + cin >> newBranch; |
| 155 | + |
| 156 | + cout << "Enter new year: "; |
| 157 | + cin >> newYear; |
| 158 | + |
| 159 | + updateDocument(collection, "rollNo", rollNo, "branch", newBranch); |
| 160 | + updateDocument(collection, "rollNo", rollNo, "year", newYear); |
| 161 | +} |
| 162 | + |
| 163 | +// Find student record. |
| 164 | +void findStudentRecord(mongocxx::collection& collection) |
| 165 | +{ |
| 166 | + string rollNo; |
| 167 | + |
| 168 | + cout << "Enter roll number: "; |
| 169 | + cin >> rollNo; |
| 170 | + |
| 171 | + findDocument(collection, "rollNo", rollNo); |
| 172 | +} |
| 173 | + |
| 174 | +// Delete student record. |
| 175 | +void deleteStudentRecord(mongocxx::collection& collection) |
| 176 | +{ |
| 177 | + string rollNo; |
| 178 | + |
| 179 | + cout << "Enter roll number: "; |
| 180 | + cin >> rollNo; |
| 181 | + |
| 182 | + deleteDocument(collection, createDocument({ {"rollNo", rollNo} })); |
| 183 | +} |
| 184 | + |
| 185 | +// Print student records. |
| 186 | +void printStudentRecords(mongocxx::collection& collection) |
| 187 | +{ |
| 188 | + printCollection(collection); |
| 189 | +} |
| 190 | + |
| 191 | + |
| 192 | +// ********************************************** Main ********************************************** |
| 193 | +int main() |
| 194 | +{ |
| 195 | + if(mongoURI.to_string().empty()) |
| 196 | + { |
| 197 | + cout << "URI is empty"; |
| 198 | + return 0; |
| 199 | + } |
| 200 | + |
| 201 | + // Create an instance. |
| 202 | + mongocxx::instance inst{}; |
| 203 | + |
| 204 | + mongocxx::options::client client_options; |
| 205 | + auto api = mongocxx::options::server_api{ mongocxx::options::server_api::version::k_version_1 }; |
| 206 | + client_options.server_api_opts(api); |
| 207 | + mongocxx::client conn{ s_Cluster0_uri, client_options}; |
| 208 | + |
| 209 | + const string dbName = "StudentRecords"; |
| 210 | + const string collName = "StudentCollection"; |
| 211 | + |
| 212 | + auto dbs = getDatabases(conn); |
| 213 | + // Check if database already exists. |
| 214 | + if (!(std::find(dbs.begin(), dbs.end(), dbName) != dbs.end())) |
| 215 | + { |
| 216 | + // Create a new database & collection for students. |
| 217 | + conn[dbName]; |
| 218 | + } |
| 219 | + |
| 220 | + auto studentDB = conn.database(dbName); |
| 221 | + auto allCollections = studentDB.list_collection_names(); |
| 222 | + |
| 223 | + // Check if collection already exists. |
| 224 | + if (!(std::find(allCollections.begin(), allCollections.end(), collName) != allCollections.end())) |
| 225 | + { |
| 226 | + createCollection(studentDB, collName); |
| 227 | + } |
| 228 | + |
| 229 | + auto studentCollection = studentDB.collection(collName); |
| 230 | + |
| 231 | + // Create a menu to for user interaction |
| 232 | + int choice = -1; |
| 233 | + |
| 234 | + do while (choice != 0) |
| 235 | + { |
| 236 | + //system("cls"); |
| 237 | + cout << endl << "**************************************************************************************************************" << endl; |
| 238 | + |
| 239 | + cout << "Enter 1 to input student record" << endl; |
| 240 | + cout << "Enter 2 to update student record" << endl; |
| 241 | + cout << "Enter 3 to find student record" << endl; |
| 242 | + cout << "Enter 4 to delete student record" << endl; |
| 243 | + cout << "Enter 5 to print all student records" << endl; |
| 244 | + cout << "Enter 0 to exit" << endl; |
| 245 | + cout << "Enter Choice : "; |
| 246 | + cin >> choice; |
| 247 | + |
| 248 | + cout << endl; |
| 249 | + |
| 250 | + switch (choice) |
| 251 | + { |
| 252 | + case 1: |
| 253 | + inputStudentRecord(studentCollection); |
| 254 | + break; |
| 255 | + case 2: |
| 256 | + updateStudentRecord(studentCollection); |
| 257 | + break; |
| 258 | + case 3: |
| 259 | + findStudentRecord(studentCollection); |
| 260 | + break; |
| 261 | + case 4: |
| 262 | + deleteStudentRecord(studentCollection); |
| 263 | + break; |
| 264 | + case 5: |
| 265 | + printStudentRecords(studentCollection); |
| 266 | + break; |
| 267 | + case 0: |
| 268 | + break; |
| 269 | + default: |
| 270 | + cout << "Invalid choice" << endl; |
| 271 | + break; |
| 272 | + } |
| 273 | + |
| 274 | + } while (choice != 0); |
| 275 | + |
| 276 | + return 0; |
| 277 | +} |
0 commit comments