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

Class representing a MongoDB database. More...

#include <database.hpp>

Public Member Functions

 database () noexcept
 Default constructs a new database. More...
 
 database (database &&) noexcept
 Move constructs a database.
 
databaseoperator= (database &&) noexcept
 Move assigns a database.
 
 database (const database &)
 Copy constructs a database.
 
databaseoperator= (const database &)
 Copy assigns a database.
 
 ~database ()
 Destroys a database.
 
 operator bool () const noexcept
 Returns true if the client is valid, meaning it was not default constructed or moved from.
 
bsoncxx::document::value run_command (bsoncxx::document::view_or_value command)
 Runs a command against this database. More...
 
class collection create_collection (bsoncxx::string::view_or_value name, const options::create_collection &options=options::create_collection())
 Explicitly creates a collection in this database with the specified options. More...
 
class collection create_view (bsoncxx::string::view_or_value name, bsoncxx::string::view_or_value view_on, const options::create_view &options=options::create_view())
 Creates a non-materialized view in this database with the specified options. More...
 
bsoncxx::document::value modify_collection (stdx::string_view name, const options::modify_collection &options=options::modify_collection())
 Modify an existing collection. More...
 
void drop ()
 Drops the database and all its collections. More...
 
bool has_collection (bsoncxx::string::view_or_value name) const
 Checks whether this database contains a collection having the given name. More...
 
cursor list_collections (bsoncxx::document::view_or_value filter={})
 Enumerates the collections in this database. More...
 
stdx::string_view name () const
 Get the name of this database. More...
 
void read_concern (class read_concern rc)
 Sets the read_concern for this database. More...
 
class read_concern read_concern () const
 The current read concern for this database. More...
 
void read_preference (class read_preference rp)
 Sets the read_preference for this database. More...
 
class read_preference read_preference () const
 The current read preference for this database. More...
 
void write_concern (class write_concern wc)
 Sets the write_concern for this database. More...
 
class write_concern write_concern () const
 The current write_concern for this database. More...
 
class collection collection (bsoncxx::string::view_or_value name) const
 Access a collection (logical grouping of documents) within this database. More...
 
class collection operator[] (bsoncxx::string::view_or_value name) const
 Allows the db["collection_name"] syntax to be used to access a collection within this database. More...
 

Detailed Description

Class representing a MongoDB database.

Acts as a gateway for accessing collections that are contained within a database. It inherits all of its default settings from the client that creates it.

Todo:
Add auth functions (add_user, remove_all_users, remove_user)

Constructor & Destructor Documentation

◆ database()

mongocxx::database::database ( )
noexcept

Default constructs a new database.

The database is not valid for use and is equivalent to the state of a moved-from database. The only valid actions to take with a default constructed database are to assign to it, or destroy it.

Member Function Documentation

◆ collection()

class collection mongocxx::database::collection ( bsoncxx::string::view_or_value  name) const

Access a collection (logical grouping of documents) within this database.

Parameters
namethe name of the collection to get.
Returns
the collection.

◆ create_collection()

class collection mongocxx::database::create_collection ( bsoncxx::string::view_or_value  name,
const options::create_collection options = options::create_collection() 
)

Explicitly creates a collection in this database with the specified options.

See also
https://docs.mongodb.com/master/reference/method/db.createCollection/
Parameters
namethe new collection's name.
optionsthe options for the new collection.
Exceptions
mongocxx::operation_exceptionif the operation fails.
Note
In order to pass a write concern to this, you must use the database level set write concern - database::write_concern(wc). (MongoDB 3.4+)

◆ create_view()

class collection mongocxx::database::create_view ( bsoncxx::string::view_or_value  name,
bsoncxx::string::view_or_value  view_on,
const options::create_view options = options::create_view() 
)

Creates a non-materialized view in this database with the specified options.

Non-materialized views are represented by the collection objects, and support many of the same read-only operations that regular collections do.

See also
https://docs.mongodb.com/master/core/views/
Parameters
namethe name of the view to be created.
view_onthe name of the source view or collection in this database from which to create the view.
optionsthe options for the new view.
Exceptions
mongocxx::operation_exceptionif the operation fails.
Note
In order to pass a write concern to this, you must use the database level set write concern - database::write_concern(wc). (MongoDB 3.4+)

◆ drop()

void mongocxx::database::drop ( )

Drops the database and all its collections.

Exceptions
mongocxx::operation_exceptionif the operation fails.
See also
https://docs.mongodb.com/master/reference/method/db.dropDatabase/
Note
In order to pass a write concern to this, you must use the database level set write concern - database::write_concern(wc). (MongoDB 3.4+)

◆ has_collection()

bool mongocxx::database::has_collection ( bsoncxx::string::view_or_value  name) const

Checks whether this database contains a collection having the given name.

Parameters
namethe name of the collection.
Returns
bool whether the collection exists in this database.
Exceptions
mongocxx::operation_exceptionif the underlying 'listCollections' command fails.

◆ list_collections()

cursor mongocxx::database::list_collections ( bsoncxx::document::view_or_value  filter = {})

Enumerates the collections in this database.

Parameters
filterAn optional query expression to filter the returned collections.
Returns
mongocxx::cursor containing the collection information.
Exceptions
mongocxx::operation_exceptionif the underlying 'listCollections' command fails.
See also
https://docs.mongodb.com/master/reference/command/listCollections/

◆ modify_collection()

bsoncxx::document::value mongocxx::database::modify_collection ( stdx::string_view  name,
const options::modify_collection options = options::modify_collection() 
)

Modify an existing collection.

Deprecated:
This method is deprecated. To modify an existing collection, invoke the "collMod" command with database::run_command().
See also
https://docs.mongodb.com/master/reference/command/collMod/
Parameters
namethe name of the collection to be modified.
optionsthe modifications to be performed.
Returns
the result of executing the command.

◆ name()

stdx::string_view mongocxx::database::name ( ) const

Get the name of this database.

Returns
the name of this database.

◆ operator[]()

collection mongocxx::database::operator[] ( bsoncxx::string::view_or_value  name) const

Allows the db["collection_name"] syntax to be used to access a collection within this database.

Parameters
namethe name of the collection to get.
Returns
the collection.

◆ read_concern() [1/2]

class read_concern mongocxx::database::read_concern ( ) const

The current read concern for this database.

If the read_concern is not explicitly set on this database object, it inherits the read_concern from its parent client object.

Returns
the current read_concern

◆ read_concern() [2/2]

void mongocxx::database::read_concern ( class read_concern  rc)

Sets the read_concern for this database.

Note
Modifications at this level do not affect existing collection instances that have come from this database, but do affect new ones. New collections will receive a copy of the new read_concern for this database upon instantiation.
Parameters
rcThe new read_concern
See also
https://docs.mongodb.com/master/reference/read-concern/

◆ read_preference() [1/2]

class read_preference mongocxx::database::read_preference ( ) const

The current read preference for this database.

See also
https://docs.mongodb.com/master/core/read-preference/
Returns
the current read_preference

◆ read_preference() [2/2]

void mongocxx::database::read_preference ( class read_preference  rp)

Sets the read_preference for this database.

Note
Modifications at this level do not affect existing collection instances that have come from this database, but do affect new ones. New collections will receive a copy of the new read_preference for this database upon instantiation.
See also
https://docs.mongodb.com/master/core/read-preference/
Parameters
rpthe new read_preference.

◆ run_command()

bsoncxx::document::value mongocxx::database::run_command ( bsoncxx::document::view_or_value  command)

Runs a command against this database.

See also
https://docs.mongodb.com/master/reference/method/db.runCommand/
Parameters
commanddocument representing the command to be run.
Returns
the result of executing the command.
Exceptions
mongocxx::operation_exceptionif the operation fails.

◆ write_concern() [1/2]

void mongocxx::database::write_concern ( class write_concern  wc)

Sets the write_concern for this database.

Note
Modifications at this level do not affect existing collection instances that have come from this database, but do affect new ones as new collections will receive a copy of the write_concern of this database upon instantiation.

◆ write_concern() [2/2]

class write_concern mongocxx::database::write_concern ( ) const

The current write_concern for this database.

Returns
the current write_concern

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