Copy # Use root/example as user/password credentials
version : '3.1'
services :
mongo :
image : mongo
restart : always
volumes :
- "./datadir:/data/db"
ports :
- 27017:27017
environment :
MONGO_INITDB_ROOT_USERNAME : root
MONGO_INITDB_ROOT_PASSWORD : example
mongo-express :
image : mongo-express
restart : always
ports :
- 8081:8081
environment :
ME_CONFIG_MONGODB_ADMINUSERNAME : root
ME_CONFIG_MONGODB_ADMINPASSWORD : example
mongo-express 為網頁視覺 DB 資料介面,但其預設沒提供帳號登入的頁面
Copy Any files with a .sh extension will be executed as shell scripts .
Any files with a .js extension will be executed as JavaScript files using the MongoDB shell .
Any files with a .json extension will be imported into the database using mongoimport .
Copy dockercompose
services :
# mongo :
# image : mongo : 4.4
# ports :
# - "27019:27017"
# environment :
# - MONGO_INITDB_DATABASE = eyedeal
# - MONGO_INITDB_ROOT_USERNAME = root
# - MONGO_INITDB_ROOT_PASSWORD = password
# volumes :
# - . / mongodb : / docker - entrypoint - initdb .d
# - . / mongodb / data : / data / db
Copy db = db .getSiblingDB ( 'testDB' );
db .createCollection ( 'test1' );
db . shops .insertMany ([
{
id : '1' ,
name : 'test1' ,
} ,
{
id : '2' ,
name : 'test2' ,
} ,
]);