Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save httpJunkie/e3e25715fd1326246263ff5b00ca9e7c to your computer and use it in GitHub Desktop.
Save httpJunkie/e3e25715fd1326246263ff5b00ca9e7c to your computer and use it in GitHub Desktop.
Get started with Docker, Mongo, Express, and Mongoose
  1. pull the official image:
docker pull mongo

optionally pull by version:

docker pull mongo:4.2.3
  1. Run in detached mode (background)
docker run -d -p 27017-27019:27017-27019 --name m1 mongo

optionally run by version:

docker run -d -p 27017-27019:27017-27019 --name m1 mongo:4.2.3
  1. Execute shell client in interactive mode:
docker exec -it m1 bash
  1. Create a directory to hold out Express app
mkdir graphql-mongoose && cd $_ && npm init -y
  1. add our dependencies
npm i express mongoose
  1. Create a server.js file
const mongoose = require('mongoose')
const { model, Schema } = mongoose

const app = require('express')()

mongoose.connect('mongodb://localhost:27017/relation', { useNewUrlParser: true })

const Department = model('department',
  new Schema({
    name: String,
    location: String
  }))

const Employee = model('employee',
  new Schema({
    firstName: String,
    lastName: String,
    mobile: String,
    department: { type: Schema.Types.ObjectId, ref: 'department' }
  }))

const Company = model('company',
  new Schema({
    name: String,
    address: String,
    employees: [{ type: Schema.Types.ObjectId, ref: 'employee' }]
  }))

app.use("/", async (req, res) => {

  await Department.remove({})
  await Department.create({
    name: 'IT Department',
    location: 'Biliding A'
  })
  await Department.create({
    name: 'Marketng Department',
    location: 'Biliding B'
  })

  await Employee.remove({})
  await Employee.create({
    firstName: 'eric',
    lastName: 'bishard',
    mobile: '321-123-0001',
    department: await Department.findOne({ name: 'IT Department' })
  })
  await Employee.create({
    firstName: 'gina',
    lastName: 'bishard',
    mobile: '321-123-0002',
    department: await Department.findOne({ name: 'Marketng Department' })
  })

  await Company.remove({})
  await Company.create({
    name: 'Bigco Ink',
    address: '123 Anywhere St',
    employees: await Employee.find()
  })

  res.json({
    deartments: await Department.find(),
    employees: await Employee.find(),
    employeesWithDepartmentNested: await Employee.find().populate('department', 'name'),
    company: await Company.find(),
    companyWithEmployeesAndDepartmentsNested: 
      await Company.find()
        .populate(
          {
            path: 'employees', 
            model: 'employee', 
            populate: {
              path: 'department', 
              model: 'department'
            }
          }),
  })
})
app.listen(7777, () => console.log("running on 7777"))
  1. Run our server.js file
node server
  1. Visit localhost:7777 to see our results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment