Repository navigation
Expand file tree
/
Copy pathtest.cpp
More file actions
51 lines (46 loc) · 1.68 KB
/
Copy pathtest.cpp
File metadata and controls
51 lines (46 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
using namespace mongocxx;
using namespace bsoncxx::builder::stream;
int main() {
try {
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{"mongodb://localhost:27017"}};
auto db = conn["healthcare_db"];
auto patients = db["patients"];
// Test: Add a patient
auto doc = document{}
<< "patient_id" << "TEST001"
<< "name" << "Test Patient"
<< "gender" << "Male"
<< "age" << 25
<< "phone" << "1234567890"
<< "email" << "test@example.com"
<< "allergies" << open_array << "None" << close_array
<< "medical_history" << open_array << "None" << close_array
<< "condition" << "Test"
<< "visit_date" << "2025-06-01"
<< finalize;
patients.insert_one(doc.view());
// Test: Retrieve patient
auto cursor = patients.find(document{} << "patient_id" << "TEST001" << finalize);
bool found = false;
for (auto&& doc : cursor) {
found = true;
std::cout << "Test passed: Patient found - " << bsoncxx::to_json(doc) << std::endl;
}
if (!found) {
std::cout << "Test failed: Patient not found\n";
return 1;
}
// Clean up
patients.delete_one(document{} << "patient_id" << "TEST001" << finalize);
return 0;
} catch (const mongocxx::exception& e) {
std::cerr << "Test failed: MongoDB error - " << e.what() << std::endl;
return 1;
}
}