diff --git a/docker-compose.yml b/docker-compose.yml index 71c14eb..2b9f83d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,4 +10,6 @@ services: environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: pass - MONGO_INITDB_DATABASE: test \ No newline at end of file + MONGO_INITDB_DATABASE: test + volumes: + - ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro \ No newline at end of file diff --git a/docker-entrypoint-initdb.d/mongo-init.js b/docker-entrypoint-initdb.d/mongo-init.js new file mode 100644 index 0000000..002945e --- /dev/null +++ b/docker-entrypoint-initdb.d/mongo-init.js @@ -0,0 +1,54 @@ +db = db.getSiblingDB('test'); + +db.createCollection('posts'); +db.createCollection('comments'); + +db.posts.insertMany([ + { + "author": { + "id": 1, + "name": "Dostoyevski" + }, + "likes": 12, + "title": "Crime and Punishment" + }, + { + "author": { + "id": 1, + "name": "Dostoyevski" + }, + "likes": 100, + "title": "Notes From The Underground" + }, + { + "author": { + "id": 2, + "name": "Marcus Aurelius" + }, + "likes": 200, + "title": "Meditations" + } +]); + +db.comments.insertMany([ + { + "comment": "great read", + "likes": 3, + "postTitle": "Crime and Punishment" + }, + { + "comment": "good info", + "likes": 0, + "postTitle": "Notes From The Underground" + }, + { + "comment": "I liked this post", + "likes": 12, + "postTitle": "Notes From The Underground" + }, + { + "comment": "very nice book", + "likes": 8, + "postTitle": "Meditations" + } +]) \ No newline at end of file diff --git a/readme.md b/readme.md index b69d2c8..ca5fa75 100644 --- a/readme.md +++ b/readme.md @@ -44,6 +44,8 @@ first create the mongo database. docker-compose up -d mongo ``` +It will generate a new MongoDB container and populate the collections with some data. + then run ```bash