Skip to content

Instantly share code, notes, and snippets.

@mariomui
Created July 23, 2019 04:04
Show Gist options
  • Save mariomui/801ffde68b805c678dab0c43bf3cb800 to your computer and use it in GitHub Desktop.
Save mariomui/801ffde68b805c678dab0c43bf3cb800 to your computer and use it in GitHub Desktop.
M220JS: MongoDB for Javascript Developers

M220JS

Using the Course without Cloud DBs

  • As a user I want to complete this course without using my cloud or using the local version of the db.

Overview

The instructions here will both be imperative and declarative. It wont have any answers to the course. It will just be a collection of thoughts and deep dives into the code.

Pre-stuff

  • download mflix from the handouts
  • unzip it. and go to that folder
  • npm install

Docker Version

  • Get the image
    • docker pull mongo:4.04 or just pull the latest
  • run the image with a few settings
    • docker run -d -p 27018:27017 /
      --name m220js /
      --v $(pwd)\input:\db\configdb /
      --v course-m220js-data:\db\data /
      mongo
    • -p <host_port>:<container_port> — map host port to container (publish)

      • that means my computer's port (host) will transfer any requests over to 27017 on the container. I have mongodb installed and the default port is already taken over by 27017.
      • -d means detached so you don't get weird logging and you can still use the terminal afterwards
      • --v [lhs:rhs] (save the data so your container can go down but this memory card stays in place)
  • mongorestore --gzip --uri --drop \
    mongodb://localhost:27018 data
    
    • load into our mongo database everything from the data folder using gzip. Since the folder inside of data is called mflix, your db name is also called mflix
      • ---drop means drop any database called mflix if it finds it then mongorestore (load the data);
      • confirm:
        mongo mongodb://localhost:27018
        show dbs;
        use mflix;
        show collections;
  • Go to your app's root directory
  • the next step is to run the app's server
    • checkout out what npm start does.
    • "start": "nodemon -L ./index.js",
      • -L means that if i had mounted my app in a container, this -L shit will make it work.
      • Now let's checkout index.js
      • it has @babel/register · Babel which transpiles any es5 stuff on the fly.
      • the exports export everything from the src file { all src stuff }. this is a little crazy that all functions are activated when you export them.

Notes:

When you stop a docker container from spinning, the volume is cached cuz docker is insane. Either do this or add the --rm when you spin up the container. Docker — Clean Up After Yourself! - Yohan Liyanage - Medium

@mariomui
Copy link
Author

It's also a good idea to remember that volumes persist if it has any data in them even if the --rm exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment