Databases - MongoDB

MongoDB is a Database system that you can use to connect your Discord bot to a databasing system!

Did you join the SneakyHub discord server yet? If not, click here to join! Be sure that you also have a Discord bot already created. If not, refer to How to Create Server

==========================

MongoDB Setup

We need to setup MongoDB before we continue. To do so, go to MongoDB and signup / sign in. Once you've done that, fill out the first questions they ask until you appear onto a screen that's asking to deploy a database. For this tutorial, I will be using the M0 plan. Go ahead and click that. That will present some options if you scroll down. The main things you can change are the Provider, Region, and Name ( Figure 1A ). After you filled in all the fields, go ahead and press the Create button.

After, you'll be brough to this screen ( Figure 1B ). There are 2 customizable fields and 1 required one. You can choose what you want for the username and password. Be sure to click the Create User button. But where it asks to add whitelisted IP addresses, put in 0.0.0.0/0 so you are able to connect it to the Discord bot.

After it's filled out, press the green Finish and Close button to finish ( Figure 1C ). When asked about the setting up acccess rules, go ahead and click Go to Overview.

Click the green Connect button and press Drivers.( Figure 1D ) Copy the Step 3 code. You'll need that soon.

Time to start linking MongoDB to SneakyHub. Go to the Panel and go to your Discord bot server. Go to the Startup tab. Find the Additional Node Packages variable ( Figure 2A ). Enter into there mongoose.

Now, go to your main Discord bot files. At the top of your file, add this line of code:

const mongoose = require('mongoose');

After, enter this other code anywhere in your code:

(async () => {
  try {
    mongoose.set('strictQuery', false);
    await mongoose.connect('PUT_YOUR_DRIVER_FROM_MONGO_HERE', { keepAlive: true });
    console.log('Connected to DB.');

    eventHandler(client);
  } catch (error) {
    console.log(`Error: ${error}`);
  }
})();

Be sure to enter your Driver information from MongoDB. Be sure after copying the connection string that you replace the password with the password you chose when generating the user.

After, go ahead and start your bot. If you set it up correctly, you should have this appear in console:

Troubleshooting

  1. Can't remember your password? Just go to Database Access => Database Users => Edit => Edit Password. After, go ahead to re-do your password. If you don't have a specific password, just press the Autogenerate Secure Password and it will automatically generate a new password for you.

Last updated

The hosting company she told you not to worry about - SneakyHub 2021-2024