项目作者: oatpp

项目描述 :
Oat++ native BSON + MongoDB driver implementation based on Oat++ object-mapping sub-framework.
高级语言: C++
项目地址: git://github.com/oatpp/oatpp-mongo.git
创建时间: 2020-04-15T21:39:50Z
项目社区:https://github.com/oatpp/oatpp-mongo

开源协议:Apache License 2.0

下载


oatpp-mongo Build Status


NOTE:

  • BSON ObjectMapper - ready-to-use.
  • 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.
    To work with MongoDB - use BSON ObjectMapper + mongocxx driver.

oatpp-mongo is the oatpp native client for MongoDB. It contains DTO to BSON mapper plus database driver.

Find the complete example project using oatpp-mongo here

More about Oat++:

How To Build

oatpp-mongo has no extrernal dependencies (The main oatpp module is still required).
libmongoxcc is used (and linked) in module tests only. Use -DOATPP_BUILD_TESTS=OFF option to build without tests and without dependency on libmongoxcc.

Install oatpp-mongo

  • Clone this repository.
  • In the root of the repository run:

    1. mkdir build && cd build
    2. cmake -DOATPP_BUILD_TESTS=OFF ..
    3. make install

API

Temporary API (using libmongoxcc)

Since oatpp driver is not ready yet, you can use libmongoxcc together with oatpp BSON.

Why using oatpp BSON? - because it’s based on oatpp object-mapping framework and
it’s extremely easy to use.

Create bsonxx::document From Any oatpp Object

  1. /**
  2. * This is the utility function that you'll need while working libmongoxcc
  3. */
  4. bsoncxx::document::value Database::createMongoDocument(const oatpp::Void &polymorph) {
  5. // if you have huge docs, you may want to increase starting BufferOutputStream size.
  6. // Or you may want to use oatpp::data::stream::ChunkedBuffer instead - for no-copy growth.
  7. oatpp::data::stream::BufferOutputStream stream;
  8. m_objectMapper.write(&stream, polymorph); //< Serialize oatpp object to BSON.
  9. bsoncxx::document::view view(stream.getData(), stream.getCurrentPosition());
  10. return bsoncxx::document::value(view);
  11. }

Where m_objectMapper - is oatpp::mongo::bson::mapping::ObjectMapper.

Insert Document

Let’s say you have such DTO defined:

  1. class User : public oatpp::DTO {
  2. DTO_INIT(User, DTO)
  3. DTO_FIELD(String, _id);
  4. DTO_FIELD(String, username);
  5. DTO_FIELD(Boolean, active);
  6. DTO_FIELD(String, role);
  7. };

Then you can insert your DTO in the database like this:

  1. collection.insert_one(createMongoDocument(myDto));

You can also insert an arbitrary document using oatpp::Any

  1. collection.insert_one(createMongoDocument(
  2. oatpp::Fields<oatpp::Any>({
  3. {"username", oatpp::String("Mr. Porridge")},
  4. {"role", oatpp::String("Admin")},
  5. {"jacket-color", oatpp::List<oatpp::String>({"red", "green", "blue"})}
  6. })
  7. ));

Read Document

Let’s say we have the same DTO - User:

  1. auto result =
  2. collection.find_one(createMongoDocument( // <-- Filter
  3. oatpp::Fields<oatpp::String>({
  4. {"_id", oatpp::String("<id-to-find>")}
  5. })
  6. ));
  7. if(result) {
  8. auto view = result->view();
  9. auto bson = oatpp::String((const char*)view.data(), view.length(), false /* to not copy view data */);
  10. auto user = m_objectMapper.readFromString<oatpp::Object<User>>(bson);
  11. // TODO - do somthing with user:)
  12. // You can then serialize it to JSON using oatpp::parser::json::mapping::ObjectMapper
  13. }

Examples