Tutorial
MongoDB Registering

Install & Setup MongoDB

Setup MongoDB

  1. Go to the MongoDB website https://cloud.mongodb.com/ (opens in a new tab)

  2. Follow the steps which are shown in the video below to create a cluster and connect to it.

Server setup

Let's say you're developing a web application that connects to a database. You need to specify the database credentials, such as the database name, username, and password, in your application code. However, hardcoding these credentials in your code can be a security risk, especially if you're sharing your code with others.

To avoid this, you can use a dotenv file to store your database credentials as environment variables. You can create a .env file in your project directory and add the database credentials as key-value pairs, like this:

DB_NAME=mydatabase
DB_USER=myusername
DB_PASSWORD=mypassword

Then, in your application code, you can load these environment variables from the dotenv file using a library like dotenv. For example, in Node.js, you can load the environment variables like this:

require('dotenv').config();
 
const dbConfig = {
  dbName: process.env.DB_NAME,
  dbUser: process.env.DB_USER,
  dbPassword: process.env.DB_PASSWORD
};
 
// Use the database credentials in your application code

This way, your database credentials are stored securely in a separate file and can be easily updated or shared with others without revealing them in your code.

  1. Create a file called .env to store environment variables for your server.
  2. Inside the .env file, define your MongoDB connection string and server port as follows:
MONGO_URL = 'mongodb+srv://your-username:your-password@cluster0.your-hostname.mongodb.net/?retryWrites=true&w=majority'
PORT = 3001

Replace "your-username" and "your-password" with the actual username and password you use to authenticate with your MongoDB cluster, and replace "your-hostname" with the hostname provided by your MongoDB cluster provider.

By using valid credentials and hostname, you can establish a secure and reliable connection to your MongoDB instance.

Mongoose setup

GalaxyGossip
 ├── server
    ├── index.js
/* MONGOOSE SETUP */
const PORT = process.env.PORT || 6001;
mongoose
  .connect(process.env.MONGO_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => {
    app.listen(PORT, () => console.log(`Server Port: ${PORT}`));
 
    /* ADD DATA ONE TIME */
    // User.insertMany(users);
    // Post.insertMany(posts);
  })
  .catch((error) => console.log(`${error} did not connect`));
const PORT = process.env.PORT || 6001;

This line creates a variable PORT that is set to the value of the PORT environment variable, or 6001 if PORT is not defined.

mongoose
  .connect(process.env.MONGO_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })

This code connects to the MongoDB database using the MONGO_URL environment variable, which should contain a valid MongoDB connection string. The second argument to the connect() method is an options object, which sets two options: useNewUrlParser and useUnifiedTopology. These options are required to avoid deprecation warnings.

  .then(() => {
    app.listen(PORT, () => console.log(`Server Port: ${PORT}`));
 

This code sets up an HTTP server to listen on the specified PORT. When the server is ready, a message is logged to the console indicating the port number.

    /* ADD DATA ONE TIME */
    // User.insertMany(users);
    // Post.insertMany(posts);
  })

This code is commented out, but it appears to be a snippet of code to insert some data into the MongoDB database using Mongoose. The User and Post variables are presumably Mongoose models, and users and posts are arrays of data to be inserted into the database.

  .catch((error) => console.log(`${error} did not connect`));

This code sets up an error handler for the MongoDB connection. If an error occurs, a message is logged to the console.

If you wanna start the server, run the following command in the terminal:

 nodemon index.js