MongoDB C++ Driver  mongocxx-3.0.2
Public Member Functions | List of all members
mongocxx::collection Class Reference

Class representing server side document groupings within a MongoDB database. More...

#include <collection.hpp>

Public Member Functions

 collection () noexcept
 Default constructs a collection object. More...
 
 collection (collection &&) noexcept
 Move constructs a collection.
 
collectionoperator= (collection &&) noexcept
 Move assigns a collection.
 
 collection (const collection &)
 Copy constructs a collection.
 
collectionoperator= (const collection &)
 Copy assigns a collection.
 
 ~collection ()
 Destroys a collection.
 
 operator bool () const noexcept
 Returns true if the collection is valid, meaning it was not default constructed or moved from.
 
cursor aggregate (const pipeline &pipeline, const options::aggregate &options=options::aggregate())
 Runs an aggregation framework pipeline against this collection. More...
 
template<typename container_type >
stdx::optional< result::bulk_writebulk_write (const container_type &writes, const options::bulk_write &options=options::bulk_write())
 Sends a container of writes to the server as a bulk write operation. More...
 
template<typename write_model_iterator_type >
stdx::optional< result::bulk_writebulk_write (write_model_iterator_type begin, write_model_iterator_type end, const options::bulk_write &options=options::bulk_write())
 Sends writes starting at begin and ending at end to the server as a bulk write operation. More...
 
stdx::optional< result::bulk_writebulk_write (const class bulk_write &bulk_write)
 Sends a batch of writes represented by the bulk_write to the server. More...
 
std::int64_t count (bsoncxx::document::view_or_value filter, const options::count &options=options::count())
 Counts the number of documents matching the provided filter. More...
 
bsoncxx::document::value create_index (bsoncxx::document::view_or_value keys, const options::index &options=options::index())
 Creates an index over the collection for the provided keys with the provided options. More...
 
stdx::optional< result::delete_resultdelete_many (bsoncxx::document::view_or_value filter, const options::delete_options &options=options::delete_options())
 Deletes all matching documents from the collection. More...
 
stdx::optional< result::delete_resultdelete_one (bsoncxx::document::view_or_value filter, const options::delete_options &options=options::delete_options())
 Deletes a single matching document from the collection. More...
 
cursor distinct (bsoncxx::string::view_or_value name, bsoncxx::document::view_or_value filter, const options::distinct &options=options::distinct())
 Finds the distinct values for a specified field accross the collection. More...
 
void drop ()
 Drops this collection and all its contained documents from the database. More...
 
cursor find (bsoncxx::document::view_or_value filter, const options::find &options=options::find())
 Finds the documents in this collection which match the provided filter. More...
 
stdx::optional< bsoncxx::document::valuefind_one (bsoncxx::document::view_or_value filter, const options::find &options=options::find())
 Finds a single document in this collection that match the provided filter. More...
 
stdx::optional< bsoncxx::document::valuefind_one_and_delete (bsoncxx::document::view_or_value filter, const options::find_one_and_delete &options=options::find_one_and_delete())
 Finds a single document matching the filter, deletes it, and returns the original. More...
 
stdx::optional< bsoncxx::document::valuefind_one_and_replace (bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value replacement, const options::find_one_and_replace &options=options::find_one_and_replace())
 Finds a single document matching the filter, replaces it, and returns either the original or the replacement document. More...
 
stdx::optional< bsoncxx::document::valuefind_one_and_update (bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value update, const options::find_one_and_update &options=options::find_one_and_update())
 Finds a single document matching the filter, updates it, and returns either the original or the newly-updated document. More...
 
stdx::optional< result::insert_oneinsert_one (bsoncxx::document::view_or_value document, const options::insert &options=options::insert())
 Inserts a single document into the collection. More...
 
template<typename container_type >
stdx::optional< result::insert_manyinsert_many (const container_type &container, const options::insert &options=options::insert())
 Inserts multiple documents into the collection. More...
 
template<typename document_view_iterator_type >
stdx::optional< result::insert_manyinsert_many (document_view_iterator_type begin, document_view_iterator_type end, const options::insert &options=options::insert())
 Inserts multiple documents into the collection. More...
 
cursor list_indexes () const
 Returns a list of the indexes currently on this collection. More...
 
stdx::string_view name () const
 Returns the name of this collection as a view of a null-terminated string. More...
 
void rename (bsoncxx::string::view_or_value new_name, bool drop_target_before_rename=false)
 Rename this collection. More...
 
void read_concern (class read_concern rc)
 Sets the read_concern for this collection. More...
 
class read_concern read_concern () const
 Gets the read_concern for the collection. More...
 
void read_preference (class read_preference rp)
 Sets the read_preference for this collection. More...
 
class read_preference read_preference () const
 Gets the read_preference for the collection. More...
 
stdx::optional< result::replace_onereplace_one (bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value replacement, const options::update &options=options::update())
 Replaces a single document matching the provided filter in this collection. More...
 
stdx::optional< result::updateupdate_many (bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value update, const options::update &options=options::update())
 Updates multiple documents matching the provided filter in this collection. More...
 
stdx::optional< result::updateupdate_one (bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value update, const options::update &options=options::update())
 Updates a single document matching the provided filter in this collection. More...
 
void write_concern (class write_concern wc)
 Sets the write_concern for this collection. More...
 
class write_concern write_concern () const
 Gets the write_concern for the collection. More...
 

Detailed Description

Class representing server side document groupings within a MongoDB database.

Collections do not require or enforce a schema and documents inside of a collection can have different fields. While not a requirement, typically documents in a collection have a similar shape or related purpose.

Example:

// Connect and get a collection
mongocxx::client mongo_client;
auto coll = mongo_client["database"]["collection"];

Constructor & Destructor Documentation

mongocxx::collection::collection ( )
noexcept

Default constructs a collection object.

The collection is equivalent to the state of a moved from colletion. The only valid actions to take with a default constructed collection are to assign to it, or destroy it.

Member Function Documentation

cursor mongocxx::collection::aggregate ( const pipeline pipeline,
const options::aggregate options = options::aggregate() 
)

Runs an aggregation framework pipeline against this collection.

Parameters
pipelineThe pipeline of aggregation operations to perform.
optionsOptional arguments, see mongocxx::options::aggregate.
Returns
A mongocxx::cursor with the results. If the query fails, the cursor throws mongocxx::query_exception when the returned cursor is iterated.
See also
http://docs.mongodb.org/manual/reference/command/aggregate/
template<typename container_type >
stdx::optional< result::bulk_write > mongocxx::collection::bulk_write ( const container_type &  writes,
const options::bulk_write options = options::bulk_write() 
)

Sends a container of writes to the server as a bulk write operation.

Template Parameters
containter_typeThe container type. Must meet the requirements for the container concept with a value type of model::write.
Parameters
writesA container of model::write.
optionsOptional arguments, see options::bulk_write.
Returns
The optional result of the bulk operation execution, a result::bulk_write.
Exceptions
mongocxx::bulk_write_exceptionwhen there are errors processing the writes.
See also
mongocxx::bulk_write
http://docs.mongodb.org/manual/core/bulk-write-operations/
template<typename write_model_iterator_type >
stdx::optional< result::bulk_write > mongocxx::collection::bulk_write ( write_model_iterator_type  begin,
write_model_iterator_type  end,
const options::bulk_write options = options::bulk_write() 
)

Sends writes starting at begin and ending at end to the server as a bulk write operation.

Template Parameters
write_model_iterator_typeThe container type. Must meet the requirements for the input iterator concept with a value type of model::write.
Parameters
beginIterator pointing to the first model::write to send.
endIterator pointing to the end of the writes to send.
optionsOptional arguments, see options::bulk_write.
Returns
The optional result of the bulk operation execution, a result::bulk_write.
Exceptions
mongocxx::bulk_write_exceptionwhen there are errors processing the writes.
See also
mongocxx::bulk_write
http://docs.mongodb.org/manual/core/bulk-write-operations/
stdx::optional<result::bulk_write> mongocxx::collection::bulk_write ( const class bulk_write bulk_write)

Sends a batch of writes represented by the bulk_write to the server.

Parameters
bulk_writeA bulk write which contains multiple write operations.
Returns
The optional result of the bulk operation execution, a result::bulk_write.
Exceptions
mongocxx::bulk_write_exceptionwhen there are errors processing the writes.
See also
http://docs.mongodb.org/manual/core/bulk-write-operations/
std::int64_t mongocxx::collection::count ( bsoncxx::document::view_or_value  filter,
const options::count options = options::count() 
)

Counts the number of documents matching the provided filter.

Parameters
filterThe filter that documents must match in order to be counted.
optionsOptional arguments, see mongocxx::options::count.
Returns
The count of the documents that matched the filter.
Exceptions
mongocxx::query_exceptionif the count operation fails.
See also
http://docs.mongodb.org/manual/reference/command/count/
bsoncxx::document::value mongocxx::collection::create_index ( bsoncxx::document::view_or_value  keys,
const options::index options = options::index() 
)

Creates an index over the collection for the provided keys with the provided options.

Parameters
keysThe keys for the index: {a: 1, b: -1}
optionsOptional arguments, see mongocxx::options::index.
Exceptions
mongocxx::logic_errorif the options are invalid.
mongocxx::operation_exceptionif index creation fails.
See also
http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/
stdx::optional<result::delete_result> mongocxx::collection::delete_many ( bsoncxx::document::view_or_value  filter,
const options::delete_options options = options::delete_options() 
)

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 result::delete_result.
Exceptions
mongocxx::bulk_write_exceptionif the delete fails.
See also
http://docs.mongodb.org/manual/reference/command/delete/
stdx::optional<result::delete_result> mongocxx::collection::delete_one ( bsoncxx::document::view_or_value  filter,
const options::delete_options options = options::delete_options() 
)

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 result::delete_result.
Exceptions
mongocxx::bulk_write_exceptionif the delete fails.
See also
http://docs.mongodb.org/manual/reference/command/delete/
cursor mongocxx::collection::distinct ( bsoncxx::string::view_or_value  name,
bsoncxx::document::view_or_value  filter,
const options::distinct options = options::distinct() 
)

Finds the distinct values for a specified field accross the collection.

Parameters
field_nameThe field for which the distinct values will be found.
filterDocument view representing the documents for which the distinct operation will apply.
optionsOptional arguments, see options::distinct.
Returns
mongocxx::cursor having the distinct values for the specified field. If the operation fails, the cursor throws mongocxx::query_exception when the returned cursor is iterated.
See also
http://docs.mongodb.org/manual/reference/command/distinct/
void mongocxx::collection::drop ( )

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

Exceptions
mongocxx::operation_exceptionif the operation fails.
See also
http://docs.mongodb.org/manual/reference/method/db.collection.drop/
cursor mongocxx::collection::find ( bsoncxx::document::view_or_value  filter,
const options::find options = options::find() 
)

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 options::find
Returns
A mongocxx::cursor with the results. If the query fails, the cursor throws mongocxx::query_exception when the returned cursor is iterated.
Exceptions
mongocxx::logic_errorif the options are invalid.
See also
http://docs.mongodb.org/manual/core/read-operations-introduction/
stdx::optional<bsoncxx::document::value> mongocxx::collection::find_one ( bsoncxx::document::view_or_value  filter,
const options::find options = options::find() 
)

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

Parameters
filterDocument view representing a document that should match the query.
optionsOptional arguments, see options::find
Returns
An optional document that matched the filter.
Exceptions
mongocxx::query_exceptionif the operation fails.
See also
http://docs.mongodb.org/manual/core/read-operations-introduction/
stdx::optional<bsoncxx::document::value> mongocxx::collection::find_one_and_delete ( bsoncxx::document::view_or_value  filter,
const options::find_one_and_delete options = options::find_one_and_delete() 
)

Finds a single document matching the filter, deletes it, and returns the original.

Parameters
filterDocument view representing a document that should be deleted.
optionsOptional arguments, see options::find_one_and_delete
Returns
The document that was deleted.
Exceptions
mongocxx::write_exceptionif the operation fails.
stdx::optional<bsoncxx::document::value> mongocxx::collection::find_one_and_replace ( bsoncxx::document::view_or_value  filter,
bsoncxx::document::view_or_value  replacement,
const options::find_one_and_replace options = options::find_one_and_replace() 
)

Finds a single document matching the filter, replaces it, and returns either the original or the replacement document.

Parameters
filterDocument view representing a document that should be replaced.
replacementDocument view representing the replacement for a matching document.
optionsOptional arguments, see options::find_one_and_replace.
Returns
The original or replaced document.
Exceptions
mongocxx::write_exceptionif the operation fails.
Note
In order to pass a write concern to this, you must use the collection level set write concern - collection::write_concern(wc).
stdx::optional<bsoncxx::document::value> mongocxx::collection::find_one_and_update ( bsoncxx::document::view_or_value  filter,
bsoncxx::document::view_or_value  update,
const options::find_one_and_update options = options::find_one_and_update() 
)

Finds a single document matching the filter, updates it, and returns either the original or the newly-updated document.

Parameters
filterDocument view representing a document that should be updated.
updateDocument view representing the update to apply to a matching document.
optionsOptional arguments, see options::find_one_and_update.
Returns
The original or updated document.
Exceptions
mongocxx::write_exceptionwhen the operation fails.
Note
In order to pass a write concern to this, you must use the collection level set write concern - collection::write_concern(wc).
template<typename container_type >
stdx::optional< result::insert_many > mongocxx::collection::insert_many ( const container_type &  container,
const options::insert options = options::insert() 
)

Inserts multiple documents into the collection.

If any of the documents are missing identifiers the driver will generate them.

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 meet the requirements for the container concept with a value type of model::write.
Parameters
containerContainer of a documents to insert.
optionsOptional arguments, see options::insert.
Returns
The result of attempting to performing the insert.
Exceptions
mongocxx::bulk_write_exceptionwhen the operation fails.
template<typename document_view_iterator_type >
stdx::optional< result::insert_many > mongocxx::collection::insert_many ( document_view_iterator_type  begin,
document_view_iterator_type  end,
const options::insert options = options::insert() 
)

Inserts multiple documents into the collection.

If any of the documents are missing identifiers the driver will generate them.

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
document_view_iterator_typeThe iterator type. Must meet the requirements for the input iterator concept with a value type of bsoncxx::document::view.
Parameters
beginIterator pointing to the first document to be inserted.
endIterator pointing to the end of the documents to be inserted.
optionsOptional arguments, see options::insert.
Returns
The result of attempting to performing the insert.
Exceptions
mongocxx::bulk_write_exceptionif the operation fails.

TODO: document DocumentViewIterator concept or static assert

stdx::optional<result::insert_one> mongocxx::collection::insert_one ( bsoncxx::document::view_or_value  document,
const options::insert options = options::insert() 
)

Inserts a single document into the collection.

If the document is missing an identifier (_id field) one will be generated for it.

Parameters
documentThe document to insert.
optionsOptional arguments, see options::insert.
Returns
The result of attempting to perform the insert.
Exceptions
mongocxx::bulk_write_exceptionif the operation fails.
cursor mongocxx::collection::list_indexes ( ) const

Returns a list of the indexes currently on this collection.

Returns
Cursor yielding the index specifications.
Exceptions
mongocxx::operation_exceptionif the operation fails.
See also
http://docs.mongodb.org/manual/reference/command/listIndexes/
stdx::string_view mongocxx::collection::name ( ) const

Returns the name of this collection as a view of a null-terminated string.

Returns
The name of the collection.
class read_concern mongocxx::collection::read_concern ( ) const

Gets the read_concern for the collection.

If a read_concern has not been explicitly set for this collection object, it inherits the read_concern from its parent database or client object.

Returns
The current read_concern.
void mongocxx::collection::read_concern ( class read_concern  rc)

Sets the read_concern for this collection.

Changes will not have any effect on existing cursors or other read operations which use the previously-set read concern.

Parameters
rcThe new read_concern
See also
https://docs.mongodb.org/manual/reference/read-concern/
void mongocxx::collection::read_preference ( class read_preference  rp)

Sets the read_preference for this collection.

Changes will not have any effect on existing cursors or other read operations which use the read preference.

Parameters
rpThe read_preference to set.
See also
http://docs.mongodb.org/manual/core/read-preference/
class read_preference mongocxx::collection::read_preference ( ) const

Gets the read_preference for the collection.

Returns
The current read_preference.
See also
http://docs.mongodb.org/manual/core/read-preference/
void mongocxx::collection::rename ( bsoncxx::string::view_or_value  new_name,
bool  drop_target_before_rename = false 
)

Rename this collection.

Parameters
new_nameThe new name to assign to the collection.
drop_target_before_renameWhether to overwrite any existing collections called new_name. The default is false.
Exceptions
mongocxx::operation_exceptionif the operation fails.
See also
https://docs.mongodb.org/manual/reference/command/renameCollection/
stdx::optional<result::replace_one> mongocxx::collection::replace_one ( bsoncxx::document::view_or_value  filter,
bsoncxx::document::view_or_value  replacement,
const options::update options = options::update() 
)

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

Parameters
filterDocument representing the match criteria.
replacementThe replacement document.
optionsOptional arguments, see options::update.
Returns
The result of attempting to replace a document.
Exceptions
mongocxx::bulk_write_exceptionif the operation fails.
See also
http://docs.mongodb.org/manual/reference/command/update/
stdx::optional<result::update> mongocxx::collection::update_many ( bsoncxx::document::view_or_value  filter,
bsoncxx::document::view_or_value  update,
const options::update options = options::update() 
)

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 options::update.
Returns
The result of attempting to update multiple documents.
Exceptions
mongocxx::bulk_write_exceptionif the update operation fails.
See also
http://docs.mongodb.org/manual/reference/command/update/
stdx::optional<result::update> mongocxx::collection::update_one ( bsoncxx::document::view_or_value  filter,
bsoncxx::document::view_or_value  update,
const options::update options = options::update() 
)

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 options::update.
Returns
The result of attempting to update a document.
Exceptions
mongocxx::bulk_write_exceptionif the update operation fails.
See also
http://docs.mongodb.org/manual/reference/command/update/
class write_concern mongocxx::collection::write_concern ( ) const

Gets the write_concern for the collection.

Returns
The current write_concern.
void mongocxx::collection::write_concern ( class write_concern  wc)

Sets the write_concern for this collection.

Changes will not have any effect on existing write operations.

Parameters
wcThe new write_concern to use.

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