Skip to content

Instantly share code, notes, and snippets.

@spiltbeans
Created March 10, 2023 20:37
Show Gist options
  • Save spiltbeans/2d067c3951d133109270eb016438e4e0 to your computer and use it in GitHub Desktop.
Save spiltbeans/2d067c3951d133109270eb016438e4e0 to your computer and use it in GitHub Desktop.

Table of Contents

Resetting Docker environment

To reset the Docker environment (i.e. delete images and volumes)

  1. Ensure Docker containers are down
docker-compose down
  1. Delete all containers using the following command:
docker rm -f $(docker ps -a -q)
  1. Delete all volumes using the following command:
docker volume rm $(docker volume ls -q)

Import MongoDB Data

The MongoDB data is usually a jsonArray which cannot be imported via MongoExpress GUI The solution is to use mongoimport through the cli

  1. Download data
wget <DOWNLOAD_LINK>
  1. Move data to container. Using the same terminal where the MongoDB container was run, type the following:
docker cp <SOURCE_PATH> <MONGO_CONTAINER_NAME | MONGO_CONTAINER_ID>:<DESTINATION_PATH>
  1. execute the mongoimport command

Method 1: Exec

    1. a. Use the exec command to execute the mongoimport command in the container:
docker exec <MONGO_CONTAINER_NAME | MONGO_CONTAINER_ID> mongoimport -h localhost:27017 -u <ADMIN_USERNAME> -p <ADMIN_PASSWORD> --authenticationDatabase=<CREDENTIALS_DB | admin>  -d <DATABASE_NAME> -c <COLLECTION_NAME> --type json --file <FILE_NAME> --jsonArray

Method 2: Bash

    1. a. Access bash. Using the same terminal where the MongoDB container was run, type the following:
docker exec -it <MONGO_CONTAINER_NAME | MONGO_CONTAINER_ID> bash
    1. b Use mongoImport to import data with the following command:
mongoimport -h localhost:27017 -u <ADMIN_USERNAME> -p <ADMIN_PASSWORD> --authenticationDatabase=<CREDENTIALS_DB | admin> -d <DATABASE_NAME> -c <COLLECTION_NAME> --type json --file <FILE_NAME> --jsonArray

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