Use the MongoDB Database class to create a reference to a named database using a provided MongoClient instance.
Create a new Mongo Database connection:
let database = try! MongoDatabase(
client: <MongoClient>,
databaseName: <String>
)
Once the connection is established and the database and collections have been defined, set the connection to close once completed using defer
. This is done in reverse order: close collections, then databases, then finally the client connection.
defer {
collection.close()
db.close()
client.close()
}
Drops the current database, deleting the associated data files.
database.drop()
name()
returns the name of the current database.
let name = database.name()
database.createCollection(name: <String>, options: <BSON>)
- name: String, name of collection to be created
- options: BSON document listing options for new collection
Use getCollection
to create a reference to a MongoCollection:
let collection = database.getCollection(name: <String>)
Use collectionNames
to create an array of the databases' collection names:
let collection = database.collectionNames()