{"id":19492793,"url":"https://github.com/oatpp/oatpp-mongo","last_synced_at":"2025-04-25T20:30:36.672Z","repository":{"id":56482809,"uuid":"256042708","full_name":"oatpp/oatpp-mongo","owner":"oatpp","description":"Oat++ native BSON + MongoDB driver implementation based on Oat++ object-mapping sub-framework.","archived":false,"fork":false,"pushed_at":"2024-04-21T20:07:03.000Z","size":132,"stargazers_count":6,"open_issues_count":3,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-29T22:52:30.559Z","etag":null,"topics":["bson","cpp","mongodb","oatpp","object-mapping"],"latest_commit_sha":null,"homepage":"https://oatpp.io/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oatpp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-15T21:39:50.000Z","updated_at":"2023-08-05T19:45:12.000Z","dependencies_parsed_at":"2024-11-07T00:26:29.946Z","dependency_job_id":"305a3279-1fbb-4abb-a14b-492a77c21bd4","html_url":"https://github.com/oatpp/oatpp-mongo","commit_stats":{"total_commits":50,"total_committers":3,"mean_commits":"16.666666666666668","dds":"0.18000000000000005","last_synced_commit":"0bc5e161c0deb4a58f7b9174fa1147cc4c256fc1"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oatpp%2Foatpp-mongo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oatpp%2Foatpp-mongo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oatpp%2Foatpp-mongo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oatpp%2Foatpp-mongo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oatpp","download_url":"https://codeload.github.com/oatpp/oatpp-mongo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224015659,"owners_count":17241535,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bson","cpp","mongodb","oatpp","object-mapping"],"created_at":"2024-11-10T21:23:02.085Z","updated_at":"2024-11-10T21:23:02.998Z","avatar_url":"https://github.com/oatpp.png","language":"C++","readme":"# oatpp-mongo [![Build Status](https://dev.azure.com/lganzzzo/lganzzzo/_apis/build/status/oatpp.oatpp-mongo?branchName=master)](https://dev.azure.com/lganzzzo/lganzzzo/_build/latest?definitionId=26\u0026branchName=master)\n\n---\n**NOTE:**  \n\n- BSON ObjectMapper - ready-to-use.\n- Database driver - **in development**. While you can do basic CRUD operations, it's still on POC stage. API is not ready and it's not recommended to use.\nTo work with MongoDB - use BSON ObjectMapper + mongocxx driver.\n---\n\n**oatpp-mongo** is the oatpp native client for MongoDB. It contains DTO to BSON mapper plus database driver.  \n\nFind the complete example project using **oatpp-mongo** [here](https://github.com/oatpp/example-mongodb)\n\nMore about Oat++:\n\n- [Oat++ Website](https://oatpp.io/)\n- [Oat++ Github Repository](https://github.com/oatpp/oatpp)\n\n## How To Build\n\n`oatpp-mongo` has no extrernal dependencies (*The main oatpp module is still required*).  \n`libmongoxcc` is used (and linked) in module **tests only**. Use `-DOATPP_BUILD_TESTS=OFF` option to build without tests and without dependency on `libmongoxcc`.\n\n### Install oatpp-mongo\n\n- Clone this repository. \n- In the root of the repository run:\n\n   ```bash\n   mkdir build \u0026\u0026 cd build\n   cmake -DOATPP_BUILD_TESTS=OFF ..\n   make install\n   ```\n\n## API\n\n### Temporary API (using libmongoxcc)\n\nSince oatpp driver is not ready yet, you can use `libmongoxcc` together with oatpp BSON.  \n\n**Why using oatpp BSON?** - because it's based on oatpp object-mapping framework and \nit's **extremely easy to use**.\n\n#### Create `bsonxx::document` From Any oatpp Object\n\n```cpp\n/**\n * This is the utility function that you'll need while working libmongoxcc\n */\nbsoncxx::document::value Database::createMongoDocument(const oatpp::Void \u0026polymorph) {\n  // if you have huge docs, you may want to increase starting BufferOutputStream size.\n  // Or you may want to use oatpp::data::stream::ChunkedBuffer instead - for no-copy growth.\n  oatpp::data::stream::BufferOutputStream stream;\n  \n  m_objectMapper.write(\u0026stream, polymorph); //\u003c Serialize oatpp object to BSON.\n  \n  bsoncxx::document::view view(stream.getData(), stream.getCurrentPosition());\n  return bsoncxx::document::value(view);\n}\n```\n\nWhere `m_objectMapper` - is `oatpp::mongo::bson::mapping::ObjectMapper`.\n\n#### Insert Document\n\nLet's say you have such DTO defined:\n\n```cpp\nclass User : public oatpp::DTO {\n\n  DTO_INIT(User, DTO)\n\n  DTO_FIELD(String, _id);\n  DTO_FIELD(String, username);\n  DTO_FIELD(Boolean, active);\n  DTO_FIELD(String, role);\n\n};\n```\n\nThen you can insert your DTO in the database like this:\n\n```cpp\ncollection.insert_one(createMongoDocument(myDto));\n```\n\nYou can also insert an arbitrary document using `oatpp::Any`\n\n```cpp\ncollection.insert_one(createMongoDocument(\n  oatpp::Fields\u003coatpp::Any\u003e({\n\n    {\"username\", oatpp::String(\"Mr. Porridge\")},\n    {\"role\", oatpp::String(\"Admin\")},\n    {\"jacket-color\", oatpp::List\u003coatpp::String\u003e({\"red\", \"green\", \"blue\"})}\n\n  })\n));\n```\n\n#### Read Document\n\nLet's say we have the same DTO - `User`:\n\n```cpp\n  auto result =\n    collection.find_one(createMongoDocument( // \u003c-- Filter\n      oatpp::Fields\u003coatpp::String\u003e({\n        {\"_id\", oatpp::String(\"\u003cid-to-find\u003e\")}\n      })\n    ));\n\n  if(result) {\n    auto view = result-\u003eview();\n    auto bson = oatpp::String((const char*)view.data(), view.length(), false /* to not copy view data */);\n    auto user = m_objectMapper.readFromString\u003coatpp::Object\u003cUser\u003e\u003e(bson);\n    // TODO - do somthing with user:)\n    // You can then serialize it to JSON using oatpp::parser::json::mapping::ObjectMapper\n  }\n```\n\n## Examples\n\n- [example-mongodb](https://github.com/oatpp/example-mongodb) - CRUD with MongoDB and Swagger-UI.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foatpp%2Foatpp-mongo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foatpp%2Foatpp-mongo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foatpp%2Foatpp-mongo/lists"}