This project is an experimental MongoDB helper library in C++, inspired by Mongoose in Node.js.
It simplifies CRUD operations, schema validation, and exception handling for C++ applications.
- 👤 Intended Audience
- 🌟 Features
- 📦 Installation
- 🚀 Usage
- 💡 Example Usage
⚠️ Error Handling- 📊 Sample Output
- 📜 License
- 🤝 Contributions
- 🙏 Acknowledgements
This library is designed for C++ developers who want to simplify MongoDB usage in their projects.
Expected audience: intermediate to advanced C++ developers who are already familiar with basic database concepts.
- CRUD Operations:
insert_one,insert_many,find_by_id,find_one,find,update_one,update_many,delete_one, anddelete_many. - Data Validation: Built-in
validation()method for custom rules. - TTL & createdAt Indexing: Auto-creation of TTL and timestamp indices.
- Custom Exceptions:
MongoSchemaException,MongoValidationException, andNotFoundException.
- C++17 or higher
- CMake (3.10+)
- MongoDB C++ Driver (
mongocxx3.10+ andbsoncxx) - Boost Library
On macOS with Homebrew:
brew install mongo-cxx-driverOn Linux (Ubuntu/Debian):
sudo apt-get install libmongocxx-dev libbsoncxx-dev├── include
│ ├── MongoSchema.h
│ ├── exceptions
│ │ ├── MongoSchemaException.h
│ │ ├── MongoValidationException.h
│ │ └── NotFoundException.h
├── src
│ ├── MongoSchema.cpp
│ ├── exceptions
│ │ ├── MongoSchemaException.cpp
│ │ ├── MongoValidationException.cpp
│ │ └── NotFoundException.cpp
├── CMakeLists.txt
└── README.md
📝 Note: This library assumes that you are comfortable with CMake and basic C++ class design.
It is intended for developers with at least intermediate C++ knowledge.
add_subdirectory(path/to/cpp-mongoose)
target_link_libraries(YourProject PRIVATE CppMongoose)#include "MongoSchema.h"
class UserModel : public MongoSchema {
public:
UserModel() : MongoSchema("users") {}
void initializeSchema(bsoncxx::document::view document) override {
// Map BSON document to fields
}
bool validation() const override {
// Add validation logic
return true;
}
};mongocxx::client client{mongocxx::uri{}};
UserModel user;
user.insert_one(R"({"name": "Alice", "age": 25})", client, "testdb");bsoncxx::oid id("your_object_id_here");
UserModel user;
auto result = user.find_by_id(id, client, "testdb");The library uses custom exceptions (MongoSchemaException, MongoValidationException, NotFoundException) for clear error reporting.
Example:
try {
user.insert_one("{}", client, "testdb");
} catch (const MongoValidationException& e) {
std::cerr << "Validation failed: " << e.what() << std::endl;
}When inserting a valid document:
Inserted document into collection 'users' with _id: 60c72b2f9af1a4f5d4b7c9d1
When validation fails:
Validation failed: Missing required field 'name'
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Open an issue or submit a PR.
