Mongo
The Mongo
class is the main interface from which you connect to a database.
It proxies MongoClient
so you should be able to call any methods implemented by it*.
important
* The MongoClient
methods are only proxied if a client
exists inside the Mongo
instance,
which means connect()
has been called successfully
#
Class Propertiesclient
#
The MongoClient
instance recovered after a successful connection.
url
#
The url
used for connecting to a MongoDB instance. Should look something like
Usually your MongoDB provider will let you coupy/paste this string.
connection_options
#
If an url
is not provided, you can alternatively provide an object with every "part" of the string,
to be constructed by the class itself:
Property | Required | Default | Description |
---|---|---|---|
host | Yes | No default | The host used to connect. For example mongodb.mydomain.com |
protocol | No | 'mongodb' | The protocol used for the connection. Usually mongodb or mongodb+srv |
username | No | '' | The username used for the connection |
password | No | '' | The password associated with the given username. Used for the connection |
port | No | 27017 | The port used to connect to the database |
query | No | {} | An object where every key/value pair will be parsed to create a querystring. Example: { key: 'val', key2: 'val2' } will be transformed to key=val&key2=val2 . The ? will be appended later |
database | No | null | The database used in the Mongo instance |
options
#
Any options to be used in MongoClient.connect
We recommend adding { useUnifiedTopology: true }
at the very least if you're able.
info
This library will not add options on it's own. The default is {}
.
database
#
The name of the database used in the Mongo instance. Only used to facilitate recovering the DB object later.
Mongo#constructor
#
#
SignatureMongo#constructor(properties={})
The class constructor takes any params listed above and calls this.config()
, setting them for the instance.
warning
This module is exported as a singleton. You can have access to the constructor by accessing it from the instance itself, but you won't usually need it.
config
#
#
SignatureMongo.config({ url, options, connection, database })
Takes any params listed in the signature and sets them in the instance.
options
is the list of options that will be passed to MongoClient.connect
connection
will be mapped to connection_options
.
get_connection_url
#
If a url
property has been defined, it will return that property. No checks are performed on it.
If a connection_options
object is provided in the instance, it will construct the url
from it.
connect
#
#
SignatureMongo.connect(options={})
Calls this.config()
setting any options provided in the options
parameter before the connection is attempted.
Attempts a connection to the MongoDB instance and returns a promise
disconnect
#
Attempts instance disconnection and returns a promise. Under the hood it just calls MongoClient.close()
.
db
#
#
SignatureMongo.db(name)
Returns a Database
instance, which is a proxy for Mongo driver's Db
.
If no name is provided and a database
property exists in the instance, that property is used instead.