Home ⇦
  1. Make connection to the real database on MongoDB service.
    1. Press New Project button for creating one project for many databases
    2. Push the button Build a Database for creating first DB
    3. Choose nearest region & name a cluster
    4. Go to Database Access tab for creating first user
      • Name this user & create pass (f.e. login admin, password admintest)
      • In Built-in Role point choose "Read and write to any database"
    5. Go to Network Access tab
      • Add IP Address
      • For developing from anywhere, in example of many developers, choose/press Allow Access From Anywhere and push Confirm button
    6. Back to database tab and go to Cluster link
    7. Go to Collections tab & Load a Sample Dataset for learning or push "Add My Own Data" button
    8. Name database and collection (noun in plural)
    9. To connect for database we should take a link,
      1. back to home page of Atlas (Mongo DB)
      2. Go to the necessary project
      3. Push the Connect button
      4. Choose Connect your application button, where copy link
      5. Change in link: user, password, database
      6. Database name must be between "mongodb.net/" and "?retryWrites", like ..mongodb.net/mydatabase?retryWrites..
      7. write this link to .env & name variable like DB_HOST
    10. In web-server install mongoose lib for working with mongoDb service npm i mongoose
    11. Add mongoDB connection code

      See the Pen 14 - MongoDB connection by Andrii (@imitator) on CodePen.

      Also in good practice app should be started after we have connection to mongoDB, means like
      .then(() => {
      console.log('DataBase was connected successfully');
      app.listen(PORT, console.log(`Server started on port ${PORT}`)),
      })

      After "db is connected" refactor controllers according to work with mongoDb

    12. Model & Schema for MongoDB theory, this point is used for explanation
      After successfully connection to database, we may try to add some object to our mongoDb db (only for explanation, b/c this object will be added every time in restarting application)
      • After "const mongoose = require('mongoose');" declaration make the code

        See the Pen 15 - Schema & model in web-server by Andrii (@imitator) on CodePen.

        Schema name named in singular name of collection in some of codes we may see const productSchema = new Schema({}), but this is improved for shortest code

        Model name in good practice named from big letter in singular noun, also first argument, in our case "product" named also as a name of collection, but in singular

      • After db was successfully connecter write the code

        See the Pen 16 - example of object creating in MongoDB by Andrii (@imitator) on CodePen.

        OR the same but for better practice if anything wrong
        .then(() => {
        console.log('DataBase was connected successfully');
        const newItem = {
        name: 'myBrand',
        price: '5222',
        };
        Product.create(newItem, (error, data) => {
        if (error) {
        console.log(error);
        return
        }
        console.log(data);
        } );
        })

        OR the same, bt with Better practice of async functions