template<typename T>
model class
MongoDB data model base class.
Contents
Typical usage:
class pants : public ops::model<pants> { public: COLLECTION(pants) pants() : ops::model<pants>{"test"} {} }; int main() { mongocxx::instance instance{}; auto document = pants::get("5b63f486be9ca51a9a3c0e81"); // ... return 0; }
Public static functions
- static auto get(const std::string& oid) -> T
- Get the MongoDB document corresponding to the provided ObjectId.
- static auto count() -> std::int64_t
- Return the total number of documents available in the MongoDB collection to which this model is linked.
- static auto count(bsoncxx::document::view_or_value filter, mongocxx::options::count options = mongocxx::options::count{}) -> std::int64_t
- todo
-
template<template<typename, typename> class Container = std::vector, template<typename> class Allocator = std::allocator>static auto page(std::int64_t skip = 0, std::int64_t limit = default_page_limit) -> ops::
page<T, Container<T, Allocator<T>>> - Obtain a subset of documents from a MongoDB collection.
Constructors, destructors, conversion operators
- model(mongocxx::uri&& uri, const std::string& db, const std::string& collection = T::mongodb_collection)
- Create a MongoDB document linked to a database and a collection.
- model(const std::string& db, const std::string& collection = T::mongodb_collection) explicit
- Create a MongoDB document linked to a database and a collection using the default host uri (mongodb://localhost:27017).
- model(const model& other)
- Create a copy of a MongoDB document.
Public functions
- auto operator=(const model& other) -> model&
- Copy assign to a MongoDB document.
- auto host() const -> std::string
- MongoDB host uri.
- auto db() const -> std::string
- MongoDB database name.
- auto collection() const -> std::string
- MongoDB collection name.
- void set_data(bsoncxx::document::view view)
- Set the document's data.
- void set_data(const std::string& data)
- Set the document's data.
- auto data() const -> bsoncxx::document::view
- Return a view of the document's data.
- auto to_json() const -> web::json::value
- Return the document's data serialized to a string.
- void set_oid(const std::string& oid)
- Set the document's ObjectId.
- auto oid() const -> std::string
- Get the document's ObjectId.
-
template<template<typename> class Validator>auto add_validator() -> std::shared_ptr<Validator<model<T>>>
- todo
- void fetch()
- Fetch the document's data from the database.
- void save()
- Persist the document to the database. This method can be used to:
- void remove()
- Delete the document from the database. This leaves the object in a fresh (newly constructed) state.
- auto operator[](const std::string& key) const -> bsoncxx::document::element
- Directly access the element of the document matching the given key.
Function documentation
template<typename T>
static T ops:: model<T>:: get(const std::string& oid)
Get the MongoDB document corresponding to the provided ObjectId.
Parameters | |
---|---|
oid | a valid MongoDB ObjectId string |
Returns | a document |
template<typename T>
template<template<typename, typename> class Container = std::vector, template<typename> class Allocator = std::allocator>
static ops:: page<T, Container<T, Allocator<T>>> ops:: model<T>:: page(std::int64_t skip = 0,
std::int64_t limit = default_page_limit)
Obtain a subset of documents from a MongoDB collection.
Parameters | |
---|---|
skip | offset from where MongoDB begins returning results |
limit | the maximum number of documents to return |
Returns | a STL container with a collection of documents |
template<typename T>
ops:: model<T>:: model(mongocxx::uri&& uri,
const std::string& db,
const std::string& collection = T::mongodb_collection)
Create a MongoDB document linked to a database and a collection.
Parameters | |
---|---|
uri | MongoDB host uri |
db | MongoDB database name |
collection | MongoDB collection name |
template<typename T>
ops:: model<T>:: model(const std::string& db,
const std::string& collection = T::mongodb_collection) explicit
Create a MongoDB document linked to a database and a collection using the default host uri (mongodb://localhost:27017).
Parameters | |
---|---|
db | MongoDB database name |
collection | MongoDB collection name |
template<typename T>
std::string ops:: model<T>:: host() const
MongoDB host uri.
Returns | the MongoDB host uri |
---|
template<typename T>
std::string ops:: model<T>:: db() const
MongoDB database name.
Returns | the name of the MongoDB database this document is linked to |
---|
template<typename T>
std::string ops:: model<T>:: collection() const
MongoDB collection name.
Returns | the name of the MongoDB collection this document is linked to |
---|
template<typename T>
void ops:: model<T>:: set_data(bsoncxx::document::view view)
Set the document's data.
Parameters | |
---|---|
view | a view of a document |
template<typename T>
void ops:: model<T>:: set_data(const std::string& data)
Set the document's data.
Parameters | |
---|---|
data | a serialized JSON object |
template<typename T>
void ops:: model<T>:: set_oid(const std::string& oid)
Set the document's ObjectId.
Parameters | |
---|---|
oid | a valid MongoDB ObjectId |
template<typename T>
std::string ops:: model<T>:: oid() const
Get the document's ObjectId.
Returns | the MongoDB ObjectId associated with the document |
---|
template<typename T>
template<template<typename> class Validator>
std::shared_ptr<Validator<model<T>>> ops:: model<T>:: add_validator()
todo
Returns | todo |
---|
template<typename T>
void ops:: model<T>:: save()
Persist the document to the database. This method can be used to:
- update an existing document; or to
- create a new document.
// Update a document auto document = pants::get("5b63f486be9ca51a9a3c0e81"); document.set_data("{\"key\":\"val\"}"); document.save(); // Create a new document pants document{}; document.set_data("{\"key\":\"val\"}"); document.save();
template<typename T>
bsoncxx::document::element ops:: model<T>:: operator[](const std::string& key) const
Directly access the element of the document matching the given key.
Parameters | |
---|---|
key | the key to match |