MongoDatabase
public class MongoDatabase
A MongoDB Database
-
Encoder used by this database for BSON conversions. This encoder’s options are inherited by collections derived from this database.
Declaration
Swift
public let encoder: BSONEncoder
-
Decoder whose options are inherited by collections derived from this database.
Declaration
Swift
public let decoder: BSONDecoder
-
The name of this database.
Declaration
Swift
public var name: String { get }
-
The
ReadConcern
set on this database, ornil
if one is not set.Declaration
Swift
public var readConcern: ReadConcern? { get }
-
The
ReadPreference
set on this databaseDeclaration
Swift
public var readPreference: ReadPreference? { get }
-
The
WriteConcern
set on this database, ornil
if one is not set.Declaration
Swift
public var writeConcern: WriteConcern? { get }
-
Cleans up internal state.
Declaration
Swift
deinit
-
Drops this database.
Throws
ServerError.commandError
if an error occurs that prevents the command from executing.
Declaration
Swift
public func drop() throws
-
Access a collection within this database.
Declaration
Swift
public func collection(_ name: String, options: CollectionOptions? = nil) -> MongoCollection<Document>
Parameters
name
the name of the collection to get
options
options to set on the returned collection
Return Value
the requested
MongoCollection<Document>
-
Access a collection within this database, and associates the specified
Codable
typeT
with the returnedMongoCollection
. This association only exists in the context of this particularMongoCollection
instance.Declaration
Swift
public func collection<T: Codable>(_ name: String, withType: T.Type, options: CollectionOptions? = nil) -> MongoCollection<T>
Parameters
name
the name of the collection to get
options
options to set on the returned collection
Return Value
the requested
MongoCollection<T>
-
Creates a collection in this database with the specified options.
Declaration
Swift
public func createCollection(_ name: String, options: CreateCollectionOptions? = nil) throws -> MongoCollection<Document>
Parameters
name
a
String
, the name of the collection to createoptions
Optional
CreateCollectionOptions
to use for the collectionReturn Value
the newly created
MongoCollection<Document>
-
Creates a collection in this database with the specified options, and associates the specified
Codable
typeT
with the returnedMongoCollection
. This association only exists in the context of this particularMongoCollection
instance.Throws
Throws:
ServerError.commandError
if an error occurs that prevents the command from executing.UserError.invalidArgumentError
if the options passed in form an invalid combination.EncodingError
if an error occurs while encoding the options to BSON.
Declaration
Swift
public func createCollection<T: Codable>(_ name: String, withType: T.Type, options: CreateCollectionOptions? = nil) throws -> MongoCollection<T>
Parameters
name
a
String
, the name of the collection to createoptions
Optional
CreateCollectionOptions
to use for the collectionReturn Value
the newly created
MongoCollection<T>
-
Lists all the collections in this database.
Throws
A
userError.invalidArgumentError
if the options passed are an invalid combination.Declaration
Swift
public func listCollections(options: ListCollectionsOptions? = nil) throws -> MongoCursor<Document>
Parameters
filter
a
Document
, optional criteria to filter results byoptions
Optional
ListCollectionsOptions
to use when executing this commandReturn Value
a
MongoCursor
over an array of collections -
Issues a MongoDB command against this database.
Throws
Throws:
UserError.invalidArgumentError
ifrequests
is empty.ServerError.writeError
if any error occurs while the command was performing a write.ServerError.commandError
if an error occurs that prevents the command from being performed.EncodingError
if an error occurs while encoding the options to BSON.
Declaration
Swift
@discardableResult public func runCommand(_ command: Document, options: RunCommandOptions? = nil) throws -> Document
Parameters
command
a
Document
containing the command to issue against the databaseoptions
Optional
RunCommandOptions
to use when executing this commandReturn Value
a
Document
containing the server response for the command