---
# System prepended metadata

title: MongoDB and MongoExpress in Docker

---

# MongoDB and MongoExpress in Docker

## Write `docker-compose-mongo.yaml` file
```yaml
version: '3'  # version of docker-compose
services:
  mongodb:
    image: mongo
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    volumes:
      - mongo-data:/data/db
  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
  mongo-data:
    driver: local

```

if you want to store data in your local path
you can change the content like below
```yml
version: '3'  # version of docker-compose
services:
  mongodb:
    image: mongo
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    volumes:
      - /{your-path}:/data/db
  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb
```

## Run Command
```bash
docker compose -f docker-compose-mongo.yaml up -d
```

## Open the browser `http://localhost:8081`
![image](https://hackmd.io/_uploads/SyQJwD4Ep.png)

## Using `mongoose` in js
### Install Package
```bash
$ npm i mongoose
```

### Write test js
- Note: you need to create a database and create a user and grant permisson for that user if you want use other users.
```javascript
//Import the mongoose module
var mongoose = require("mongoose");

async function main() {
  //Set up default mongoose connection
  var mongoDB = "mongodb://admin:password@127.0.0.1:27017/db?authSource=admin";
  mongoose.connect(mongoDB);
  // Get Mongoose to use the global promise library
  mongoose.Promise = global.Promise;
  //Get the default connection
  var db = mongoose.connection;
  var data = await db.collection("students").insertOne({ name: "John" });
  console.log(data);

  //Bind connection to error event (to get notification of connection errors)
  //   db.on("error", console.error.bind(console, "MongoDB connection error:"));
}
main();

```
You can also use these code below
```javascript
//Import the mongoose module
var mongoose = require("mongoose");

async function main() {
  //Set up default mongoose connection
  //   var mongoDB = "mongodb://admin:password@127.0.0.1:27017/db?authSource=admin";
  var mongoDB = "mongodb://admin:password@127.0.0.1:27017";
  var dbName = "db";
  mongoose.connect(mongoDB);
  // Get Mongoose to use the global promise library
  mongoose.Promise = global.Promise;
  //Get the default connection
  var db = mongoose.connection.useDb(dbName);
  var data = await db.collection("students").insertOne({ name: "Adem2" });
  console.log(data);

  //Bind connection to error event (to get notification of connection errors)
  //   db.on("error", console.error.bind(console, "MongoDB connection error:"));
}
main();

```