Skip to content

MongoDB

Vendor cheat sheet: https://www.mongodb.com/developer/products/mongodb/cheat-sheet/

Basics

List databases

show dbs
Current db
db 
Use
use <db-name>
Collections
show collections

dump & restore

Dump

DD=$(date +"%Y%m%d-%H%M%S")
mongodump --archive --authenticationDatabase=admin -u root -p pasword  > mondodb-${DD}.dump

Restore

mongorestore --host localhost:27017 -u root -p password --drop --archive=<mondodb_dump_file>

docker compose

services:
  mongodb:
    image: mongo
    container_name: mongodb
    restart: always
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME:root
      MONGO_INITDB_ROOT_PASSWORD:password
    volumes:
      - ./data/mongo:/data/db