#
MongoDB
#
Installation
In your project folder, you should be able to just install using this command:
npm i @joshdb/core @joshdb/mongo
** OR **
yarn add @joshdb/core @joshdb/mongo
#
Usage
Using the mongo provider goes as such:
const Josh = require("@joshdb/core");
const provider = require("@joshdb/mongo");
const myDb = new Josh({
name: 'test',
provider,
providerOptions: {
url: 'mongodb://localhost'
}
});
db.defer.then( () => {
console.log(`Connected, there are ${db.count} rows in the database.`);
});
#
Provider Options
Let's try to make some sense of the options. In the above example I use the URL for the connection, using a Mongo Atlas cluster.
Here is a list of full options this provider supports:
#
Mongo Atlas Configuration
Mongo Atlas is a free mongodb hosting service made by, if that wasn't obvious, the Mongo people. While there are some limitations to the free service, it's still very useable for any small implementation.
The setup for Mongo Atlas goes something like this:
- Get an account at mongodb.com
- Once created, setup your cluster:
- Provider & Region: Up to you, I chose AWS in my test (but you might have a closer free region!). Make sure to select a "FREE TIER AVAILABLE" region!
- Keep the M0 cluster tier (the only free one). You may select backups if you want, other additional options are paid.
- Type in a cluster name of your choice. Something like
guidebot-cluster
. - Click Create Cluster.
- Go make a sandwich, setup can take a while...
- Click the Collection button in the middle of the page.
- Click Create Database. Enter a name such as
guidebot-session
then a collection name such assessions
.
- Now that the cluster is created, we now need to get a connection string. But we need to create a user, which there's a wizard for.
- Click Command Line Tools at the top of the cluster window, then click Connect Instructions.
- Click Add your Current IP if you're on the machine that will host your bot. Otherwise, click Add a Different IP Address and enter it on the left. Click Add IP Address.
- Enter a database access username and password, then click Create MongoDB User.
- Click Choose a connection method.
- Click Connect your Application
- Change the driver version to 3.0 or later.
- Your connection string is here! Keep the page open to copy it later in the setup stage.