Mangrove
The C++ Object Document Mapper for MongoDB
Public Member Functions | Static Public Member Functions | List of all members
mangrove::model< T, IdType > Class Template Reference

Public Member Functions

template<typename... Ts, typename = std::enable_if_t<!first_two_types_are_same<model, Ts...>::value>>
 model (Ts &&...ts)
 Forward the arguments to the constructor of IdType. More...
 
void remove ()
 Deletes this object from the underlying collection. More...
 
void save ()
 Performs an update in the database that saves the current T object instance to the collection mapped to this class. More...
 

Static Public Member Functions

static std::int64_t count (bsoncxx::document::view_or_value filter=bsoncxx::document::view_or_value{}, const mongocxx::options::count &options=mongocxx::options::count())
 Counts the number of documents matching the provided filter. More...
 
static const mongocxx::collection collection ()
 Returns a copy of the underlying collection. More...
 
static mongocxx::stdx::optional< mongocxx::result::delete_result > delete_many (bsoncxx::document::view_or_value filter, const mongocxx::options::delete_options &options=mongocxx::options::delete_options())
 Deletes all matching documents from the collection. More...
 
static mongocxx::stdx::optional< mongocxx::result::delete_result > delete_one (bsoncxx::document::view_or_value filter, const mongocxx::options::delete_options &options=mongocxx::options::delete_options())
 Deletes a single matching document from the collection. More...
 
static void drop ()
 Drops the underlying collection and all its contained documents from the database. More...
 
static deserializing_cursor< T > find (bsoncxx::document::view_or_value filter, const mongocxx::options::find &options=mongocxx::options::find())
 Finds the documents in this collection which match the provided filter. More...
 
static mongocxx::stdx::optional< T > find_one (bsoncxx::document::view_or_value filter, const mongocxx::options::find &options=mongocxx::options::find())
 Finds a single document in this collection that matches the provided filter. More...
 
template<typename container_type , typename = std::enable_if_t<container_of_v<container_type, T>>>
static mongocxx::stdx::optional< mongocxx::result::insert_many > insert_many (const container_type &container, const mongocxx::options::insert &options=mongocxx::options::insert())
 Inserts multiple object of the model into the collection. More...
 
template<typename object_iterator_type , typename = std::enable_if_t<iterator_of_v<object_iterator_type, T>>>
static mongocxx::stdx::optional< mongocxx::result::insert_many > insert_many (object_iterator_type begin, object_iterator_type end, const mongocxx::options::insert &options=mongocxx::options::insert())
 Inserts multiple objects of the model into the collection. More...
 
static mongocxx::stdx::optional< mongocxx::result::insert_one > insert_one (T obj, const mongocxx::options::insert &options=mongocxx::options::insert())
 Inserts a single object of the model into the collection. More...
 
static void setCollection (const mongocxx::collection &coll)
 Sets the underlying mongocxx::collection used to store and load instances of T. More...
 
static mongocxx::stdx::optional< mongocxx::result::update > update_many (bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value update, const mongocxx::options::update &options=mongocxx::options::update())
 Updates multiple documents matching the provided filter in this collection. More...
 
static mongocxx::stdx::optional< mongocxx::result::update > update_one (bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value update, const mongocxx::options::update &options=mongocxx::options::update())
 Updates a single document matching the provided filter in this collection. More...
 

Constructor & Destructor Documentation

template<typename T , typename IdType = bsoncxx::oid>
template<typename... Ts, typename = std::enable_if_t<!first_two_types_are_same<model, Ts...>::value>>
mangrove::model< T, IdType >::model ( Ts &&...  ts)
inline

Forward the arguments to the constructor of IdType.

A std::enable_if is included to disable the template for the copy constructor case so the default is used.

Parameters
tsThe variadic pack of arguments to be forwarded to the constructor of IdType.

Member Function Documentation

template<typename T , typename IdType = bsoncxx::oid>
static const mongocxx::collection mangrove::model< T, IdType >::collection ( )
inlinestatic

Returns a copy of the underlying collection.

Returns
A copy of the underlying mongocxx::collection that this class uses to store and load instances of T.
template<typename T , typename IdType = bsoncxx::oid>
static std::int64_t mangrove::model< T, IdType >::count ( bsoncxx::document::view_or_value  filter = bsoncxx::document::view_or_value{},
const mongocxx::options::count &  options = mongocxx::options::count() 
)
inlinestatic

Counts the number of documents matching the provided filter.

Parameters
filterThe filter that documents must match in order to be counted. If a filter is not provided, count() will count the number of documents in the entire collection.
optionsOptional arguments, see mongocxx::options::count.
Returns
The count of the documents that matched the filter.
Exceptions
mongocxx::exception::queryif the count operation fails.
See also
https://docs.mongodb.com/manual/reference/command/count/
template<typename T , typename IdType = bsoncxx::oid>
static mongocxx::stdx::optional<mongocxx::result::delete_result> mangrove::model< T, IdType >::delete_many ( bsoncxx::document::view_or_value  filter,
const mongocxx::options::delete_options &  options = mongocxx::options::delete_options() 
)
inlinestatic

Deletes all matching documents from the collection.

Parameters
filterDocument view representing the data to be deleted.
optionsOptional arguments, see mongocxx::options::delete_options.
Returns
The optional result of performing the deletion, a mongocxx::result::delete_result.
Exceptions
mongocxx::exception::writeif the delete fails.
See also
http://docs.mongodb.com/manual/reference/command/delete/
template<typename T , typename IdType = bsoncxx::oid>
static mongocxx::stdx::optional<mongocxx::result::delete_result> mangrove::model< T, IdType >::delete_one ( bsoncxx::document::view_or_value  filter,
const mongocxx::options::delete_options &  options = mongocxx::options::delete_options() 
)
inlinestatic

Deletes a single matching document from the collection.

Parameters
filterDocument view representing the data to be deleted.
optionsOptional arguments, see mongocxx::options::delete_options.
Returns
The optional result of performing the deletion, a mongocxx::result::delete_result.
Exceptions
mongocxx::exception::writeif the delete fails.
See also
http://docs.mongodb.com/manual/reference/command/delete/
template<typename T , typename IdType = bsoncxx::oid>
static void mangrove::model< T, IdType >::drop ( )
inlinestatic

Drops the underlying collection and all its contained documents from the database.

Exceptions
exception::operationif the operation fails.
See also
https://docs.mongodb.com/manual/reference/method/db.collection.drop/
template<typename T , typename IdType = bsoncxx::oid>
static deserializing_cursor<T> mangrove::model< T, IdType >::find ( bsoncxx::document::view_or_value  filter,
const mongocxx::options::find &  options = mongocxx::options::find() 
)
inlinestatic

Finds the documents in this collection which match the provided filter.

Parameters
filterDocument view representing a document that should match the query.
optionsOptional arguments, see mongocxx::options::find
Returns
Cursor with deserialized objects from the collection.
Exceptions
Ifthe find failed, the returned cursor will throw mongocxx::exception::query when it is iterated.
See also
https://docs.mongodb.com/manual/tutorial/query-documents/
template<typename T , typename IdType = bsoncxx::oid>
static mongocxx::stdx::optional<T> mangrove::model< T, IdType >::find_one ( bsoncxx::document::view_or_value  filter,
const mongocxx::options::find &  options = mongocxx::options::find() 
)
inlinestatic

Finds a single document in this collection that matches the provided filter.

Parameters
filterDocument view representing a document that should match the query.
optionsOptional arguments, see mongocxx::options::find
Returns
An optional object that matched the filter.
Exceptions
mongocxx::exception::queryif the operation fails.
See also
https://docs.mongodb.com/manual/tutorial/query-documents/
template<typename T , typename IdType = bsoncxx::oid>
template<typename container_type , typename = std::enable_if_t<container_of_v<container_type, T>>>
static mongocxx::stdx::optional<mongocxx::result::insert_many> mangrove::model< T, IdType >::insert_many ( const container_type &  container,
const mongocxx::options::insert &  options = mongocxx::options::insert() 
)
inlinestatic

Inserts multiple object of the model into the collection.

Warning
This method uses the bulk insert command to execute the insertion as opposed to the legacy OP_INSERT wire protocol message. As a result, using this method to insert many documents on MongoDB < 2.6 will be slow.
Template Parameters
containter_typeThe container type. Must contain an iterator that yields objects of this model.
Parameters
containerContainer of model objects to insert.
optionsOptional arguments, see mongocxx::options::insert.
See also
https://docs.mongodb.com/manual/tutorial/insert-documents/
Returns
The result of attempting to performing the insert.
Exceptions
mongocxx::exception::writewhen the operation fails.
template<typename T , typename IdType = bsoncxx::oid>
template<typename object_iterator_type , typename = std::enable_if_t<iterator_of_v<object_iterator_type, T>>>
static mongocxx::stdx::optional<mongocxx::result::insert_many> mangrove::model< T, IdType >::insert_many ( object_iterator_type  begin,
object_iterator_type  end,
const mongocxx::options::insert &  options = mongocxx::options::insert() 
)
inlinestatic

Inserts multiple objects of the model into the collection.

Warning
This method uses the bulk insert command to execute the insertion as opposed to the legacy OP_INSERT wire protocol message. As a result, using this method to insert many documents on MongoDB < 2.6 will be slow.
Template Parameters
object_iterator_typeThe iterator type. Must meet the requirements for the input iterator concept with the model class as the value type.
Parameters
beginIterator pointing to the first document to be inserted.
endIterator pointing to the end of the documents to be inserted.
optionsOptional arguments, see mongocxx::options::insert.
See also
https://docs.mongodb.com/manual/tutorial/insert-documents/
Returns
The result of attempting to performing the insert.
Exceptions
mongocxx::exception::writeif the operation fails.
template<typename T , typename IdType = bsoncxx::oid>
static mongocxx::stdx::optional<mongocxx::result::insert_one> mangrove::model< T, IdType >::insert_one ( obj,
const mongocxx::options::insert &  options = mongocxx::options::insert() 
)
inlinestatic

Inserts a single object of the model into the collection.

Parameters
objThe object of the model to insert.
optionsOptional arguments, see mongocxx::options::insert.
See also
https://docs.mongodb.com/manual/tutorial/insert-documents/
Returns
The result of attempting to perform the insert.
Exceptions
mongocxx::exception::writeif the operation fails.
template<typename T , typename IdType = bsoncxx::oid>
void mangrove::model< T, IdType >::remove ( )
inline

Deletes this object from the underlying collection.

In the terms of the CRUD specification, this uses deleteOne with the _id as the sole argument to the query filter.

See also
https://docs.mongodb.com/manual/reference/method/db.collection.deleteOne/
template<typename T , typename IdType = bsoncxx::oid>
void mangrove::model< T, IdType >::save ( )
inline

Performs an update in the database that saves the current T object instance to the collection mapped to this class.

In the terms of the CRUD specification, this uses updateOne with the _id as the sole argument to the query filter, the T object serialized to dotted notation BSON as the $set operand, and upsert=true so that objects that aren't already in the collection are automatically inserted.

See also
https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/
template<typename T , typename IdType = bsoncxx::oid>
static void mangrove::model< T, IdType >::setCollection ( const mongocxx::collection &  coll)
inlinestatic

Sets the underlying mongocxx::collection used to store and load instances of T.

Parameters
collThe mongocxx::collection object to be mapped to this class.
Warning
This must be called with a new mongocxx::collection instance for every thread for the model to be thread-safe.
The parent mongocxx::client from which the mongocxx::collection argument was created must outlive any of this model's CRUD methods. If the client object goes out of scope, a new collection must be passed to this method before using any CRUD methods.
template<typename T , typename IdType = bsoncxx::oid>
static mongocxx::stdx::optional<mongocxx::result::update> mangrove::model< T, IdType >::update_many ( bsoncxx::document::view_or_value  filter,
bsoncxx::document::view_or_value  update,
const mongocxx::options::update &  options = mongocxx::options::update() 
)
inlinestatic

Updates multiple documents matching the provided filter in this collection.

Parameters
filterDocument representing the match criteria.
updateDocument representing the update to be applied to matching documents.
optionsOptional arguments, see mongocxx::options::update.
Returns
The result of attempting to update multiple documents.
Exceptions
exception::writeif the update operation fails.
See also
http://docs.mongodb.com/manual/reference/command/update/
template<typename T , typename IdType = bsoncxx::oid>
static mongocxx::stdx::optional<mongocxx::result::update> mangrove::model< T, IdType >::update_one ( bsoncxx::document::view_or_value  filter,
bsoncxx::document::view_or_value  update,
const mongocxx::options::update &  options = mongocxx::options::update() 
)
inlinestatic

Updates a single document matching the provided filter in this collection.

Parameters
filterDocument representing the match criteria.
updateDocument representing the update to be applied to a matching document.
optionsOptional arguments, see mongocxx::options::update.
Returns
The result of attempting to update a document.
Exceptions
mongocxx::exception::writeif the update operation fails.
See also
http://docs.mongodb.com/manual/reference/command/update/

The documentation for this class was generated from the following file: