Skip to content

ohalukkarakaya/cpp-mongoose

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

thumbnail

C++ Mongoose-Like Library

A lightweight C++ library for managing MongoDB with an object-oriented approach.


📖 Overview

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.


🗺️ Table of Contents


👤 Intended Audience

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.


🌟 Features

  • CRUD Operations: insert_one, insert_many, find_by_id, find_one, find, update_one, update_many, delete_one, and delete_many.
  • Data Validation: Built-in validation() method for custom rules.
  • TTL & createdAt Indexing: Auto-creation of TTL and timestamp indices.
  • Custom Exceptions: MongoSchemaException, MongoValidationException, and NotFoundException.

📦 Installation

🔧 Prerequisites

  • C++17 or higher
  • CMake (3.10+)
  • MongoDB C++ Driver (mongocxx 3.10+ and bsoncxx)
  • Boost Library

📥 Installing MongoDB C++ Driver

On macOS with Homebrew:

brew install mongo-cxx-driver

On Linux (Ubuntu/Debian):

sudo apt-get install libmongocxx-dev libbsoncxx-dev

🗂️ Project Structure

├── include
│   ├── MongoSchema.h
│   ├── exceptions
│   │   ├── MongoSchemaException.h
│   │   ├── MongoValidationException.h
│   │   └── NotFoundException.h
├── src
│   ├── MongoSchema.cpp
│   ├── exceptions
│   │   ├── MongoSchemaException.cpp
│   │   ├── MongoValidationException.cpp
│   │   └── NotFoundException.cpp
├── CMakeLists.txt
└── README.md

🚀 Usage

📝 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.

🏗️ Adding to Your Project Using CMake

add_subdirectory(path/to/cpp-mongoose)
target_link_libraries(YourProject PRIVATE CppMongoose)

📝 Using the MongoSchema Class

#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;
    }
};

💡 Example Usage

➕ Inserting a Single Document

mongocxx::client client{mongocxx::uri{}};
UserModel user;
user.insert_one(R"({"name": "Alice", "age": 25})", client, "testdb");

🔍 Finding a Document by ID

bsoncxx::oid id("your_object_id_here");
UserModel user;
auto result = user.find_by_id(id, client, "testdb");

⚠️ Error Handling

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;
}

📊 Sample Output

When inserting a valid document:

Inserted document into collection 'users' with _id: 60c72b2f9af1a4f5d4b7c9d1

When validation fails:

Validation failed: Missing required field 'name'

📜 License

This project is licensed under the MIT License - see the LICENSE file for details.


🤝 Contributions

Contributions are welcome! Open an issue or submit a PR.


🙏 Acknowledgements

About

cpp mongoose

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published